tlivings / enjoi

Converts a JSON schema to a Joi schema.
Apache License 2.0
283 stars 57 forks source link

Undefined is always valid regardless of type #35

Closed cmawhorter closed 6 years ago

cmawhorter commented 7 years ago

All types are incorrectly reporting undefined as valid. Array is the exception because of #33

Enjoi({ type: 'string' }).validate(undefined, console.log.bind(console, 'string'));
Enjoi({ type: 'array' }).validate(undefined, console.log.bind(console, 'array'));
Enjoi({ type: 'boolean' }).validate(undefined, console.log.bind(console, 'boolean'));
Enjoi({ type: 'number' }).validate(undefined, console.log.bind(console, 'number'));
Enjoi({ type: 'integer' }).validate(undefined, console.log.bind(console, 'integer'));
Enjoi({ type: 'object' }).validate(undefined, console.log.bind(console, 'object'));
Enjoi({ type: 'string' }).validate(undefined, console.log.bind(console, 'string'));

No ValidationError for any of those and the value returned is undefined.

(Sorry for the deluge!)

tlivings commented 7 years ago

These should fail against null, but don't because undefined means there is nothing to validate against. It will fail properly if you set keys as required and pass undefined.

I see this behavior in json schema validation as well.

That's not a final answer but first thoughts.

tlivings commented 7 years ago

At the end of this day, this behavior is based on Joi. These properties are not necessarily required, but Joi passes validation because they are not marked as required. I don't think there is anything to fix here.