Closed PandaWorker closed 1 month ago
Union is also not working
const schema1 = s.fromJSON({
anyOf: [{ const: 'lt' }, { const: 'gt' }],
});
const schema2 = s.union([s.literal('lt'), s.literal('gt')]);
// { anyOf: [ undefined ] }
console.log(schema2.schema);
console.log(schema1.parse('gt')); // gt
console.log(schema1.parse('lt')); // lt
console.log(schema2.parse('gt')); // schema is invalid: data/anyOf/0 must be object,boolean
By default, all properties should be required, but here they are all optional
{
const schema1 = s.fromJSON({
type: 'object',
properties: {
name: {
type: 'string',
minLength: 2,
maxLength: 20,
},
age: {
type: 'integer',
minimum: 0,
maximum: 100,
},
email: {
anyOf: [{ type: 'string', format: 'email' }, { type: 'null' }],
default: null,
},
phone: {
anyOf: [{ type: 'string' }, { type: 'null' }],
},
surname: {
anyOf: [{ type: 'string' }, { type: 'null' }],
},
},
required: ['name', 'age', 'surname'],
additionalProperties: false,
});
const schema2 = s.object({
name: s.string().minLength(2).maxLength(20),
age: s.number().integer().min(0).max(100),
// default optional - string | null | undefined
email: s.string().format('email').nullable().optional().default(null),
//optional - string | null | undefined
phone: s.string().nullable().optional(),
// required - string | null
surname: s.string().nullable(),
}).strict();
console.log(schema1.schema);
console.log(schema2.schema);
console.log(schema1.parse({ name: 'Alex', age: 10, surname: null}));
console.log(schema2.parse({ name: 'Alex', age: 10,})); // pass? { name: 'Alex', age: 10, email: null }
}
Ajv-ts schema
{
type: 'object',
properties: {
name: { type: 'string', minLength: 2, maxLength: 20 },
age: { type: 'integer', minimum: 0, maximum: 100 },
email: {
type: [ 'string', 'null' ],
format: 'email',
nullable: true,
default: null
},
phone: { type: [ 'string', 'null' ], nullable: true },
surname: { type: [ 'string', 'null' ], nullable: true }
},
additionalProperties: false
}
@PandaWorker thanks for issue creating - I'll fix it as soon as possible
fix will be shipped on next release 🎉
Ajv supports json-schema draft-7 (2020-12), and I think it would be better to rewrite it, since it is more convenient and flexible And I think that the methods should not modify the scheme, but should return a new scheme, as is done in other validators