tlivings / enjoi

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

Schema allows empty strings #41

Closed JoeScho closed 6 years ago

JoeScho commented 6 years ago

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?

tlivings commented 6 years ago

The opposite issue existed at one point: https://github.com/tlivings/enjoi/issues/16

I wonder if Joi’s behavior has changed.

tlivings commented 6 years ago

Never mind, I see the issue.

tlivings commented 6 years ago

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

JoeScho commented 6 years ago

Thanks @tlivings 👌