Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit f72dadc

Browse files
Merge pull request #429 from apiaryio/428-typeof-null
Returns expected type "null" when given null actual data type
2 parents 888b34d + a7d1b53 commit f72dadc

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

lib/validators/json-schema-next.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ class JsonSchemaValidator {
244244

245245
switch (keyword) {
246246
case 'type':
247-
return `At '${pointer}' Invalid type: ${typeof data} (expected ${
248-
params.type
249-
})`;
247+
return `At '${pointer}' Invalid type: ${
248+
data === null ? null : typeof data
249+
} (expected ${params.type})`;
250250

251251
case 'required':
252252
return `At '${pointer}' Missing required property: ${last(property)}`;

test/unit/units/validateBody.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,45 @@ describe('validateBody', () => {
353353
});
354354
});
355355
});
356+
357+
describe('with non-matching body types', () => {
358+
const result = validateBody(
359+
{
360+
bodySchema: {
361+
properties: {
362+
firstName: {
363+
type: 'string'
364+
}
365+
}
366+
}
367+
},
368+
{
369+
body: '{ "firstName": null }'
370+
}
371+
);
372+
373+
it('marks field as invalid', () => {
374+
expect(result).to.not.be.valid;
375+
});
376+
377+
it('has "json" kind', () => {
378+
expect(result).to.have.kind('json');
379+
});
380+
381+
describe('produces an error', () => {
382+
it('exactly one error', () => {
383+
expect(result).to.have.errors.lengthOf(1);
384+
});
385+
386+
it('has explanatory message', () => {
387+
expect(result)
388+
.to.have.errorAtIndex(0)
389+
.withMessage(
390+
`At '/firstName' Invalid type: null (expected string)`
391+
);
392+
});
393+
});
394+
});
356395
});
357396
});
358397
});

test/unit/validators/json-schema.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ describe('JsonSchema', () => {
109109
});
110110
});
111111
});
112-
113-
// shared.shouldBehaveLikeAmandaToGavel(new JsonSchemaValidator('{}'));
114112
});
115113

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

0 commit comments

Comments
 (0)