tlivings / enjoi

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

Date formatting #51

Closed ahmed-medhat-tawfiq closed 6 years ago

ahmed-medhat-tawfiq commented 6 years ago
const Joi = require('joi');
const Enjoi = require('enjoi');

const schema = Enjoi({
    type: 'object',
    properties: {
        firstName: {
            description: 'First name.',
            type: 'string'
        },
        lastName: {
            description: 'Last name.',
            type: 'string'
        },
        mail: {type: "email"},
        effectiveDate: {type: 'date', minimum: '12-21-2012'},
        age: {
            description: 'Age in years',
            type: 'integer',
            minimum: 1
        }
    }
}, {
    types: {
        email: Joi.string().email(),
        date: Joi.date()
    }
});

const {error} = Joi.validate({firstName: 'John', lastName: 'Doe', age: 9, mail: "ahmed@gmail.com", effectiveDate: "2010-08-09"}, schema);

console.log(error);

i just need to add minimum and maximum to effectiveDate attribute, i know that i can add it in types to be like that date: Joi.date().min('12-21-2012') but this is a bad soln as maybe i have more than one attribute with type of date ?!!

ahmed-medhat-tawfiq commented 6 years ago

i c that Ajv pkg provide json schema v5 and it have formatMinimum and formatMaximum to add min and maz to date type for example

but they have bad error msgs not like joi and allow unknown/ extra fields by default (you need to add "additionalProperties": false for each nested object)

ahmed-medhat-tawfiq commented 6 years ago

sorry min and max date as already exist in Enjoi i found that in one of commits i don't know why u didn't mention that in in ReadMe

anyway i test that in example and i works well