Skip to content

Commit

Permalink
fix: returns expected type "null" when given null actual data type
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Feb 4, 2020
1 parent 888b34d commit a7d1b53
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/validators/json-schema-next.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ class JsonSchemaValidator {

switch (keyword) {
case 'type':
return `At '${pointer}' Invalid type: ${typeof data} (expected ${
params.type
})`;
return `At '${pointer}' Invalid type: ${
data === null ? null : typeof data
} (expected ${params.type})`;

case 'required':
return `At '${pointer}' Missing required property: ${last(property)}`;
Expand Down
39 changes: 39 additions & 0 deletions test/unit/units/validateBody.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,45 @@ describe('validateBody', () => {
});
});
});

describe('with non-matching body types', () => {
const result = validateBody(
{
bodySchema: {
properties: {
firstName: {
type: 'string'
}
}
}
},
{
body: '{ "firstName": null }'
}
);

it('marks field as invalid', () => {
expect(result).to.not.be.valid;
});

it('has "json" kind', () => {
expect(result).to.have.kind('json');
});

describe('produces an error', () => {
it('exactly one error', () => {
expect(result).to.have.errors.lengthOf(1);
});

it('has explanatory message', () => {
expect(result)
.to.have.errorAtIndex(0)
.withMessage(
`At '/firstName' Invalid type: null (expected string)`
);
});
});
});
});
});
});
Expand Down
2 changes: 0 additions & 2 deletions test/unit/validators/json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ describe('JsonSchema', () => {
});
});
});

// shared.shouldBehaveLikeAmandaToGavel(new JsonSchemaValidator('{}'));
});

describe('when validation performed on actual empty object', () => {
Expand Down

0 comments on commit a7d1b53

Please sign in to comment.