tlivings/enjoi

Schema allows empty strings

JoeScho opened this issue · 4 comments

By default, the schemas created by enjoi allow empty strings. For example:

const schema = Enjoi({
    type: 'object',
    properties: {
        firstName: {
            description: 'First name.',
            type: 'string'
        },
        lastName: {
            description: 'Last name.',
            type: 'string'
        },
        age: {
            description: 'Age in years',
            type: 'integer',
            minimum: 1
        }
    },
    'required': ['firstName', 'lastName']
});

schema.validate({
  firstName: '',
  lastName: 'Smith'
}); // Does not return an error

Is there a way of disabling this so that the above validation would fail?

The opposite issue existed at one point: #16

I wonder if Joi’s behavior has changed.

Never mind, I see the issue.

So, according to JSON schema specification, an empty string validates as a string, so this is expected behavior. To change, use minLength.

Thanks @tlivings 👌