Validate nested values against a schema and report all errors
Form builders, API gateways, and config loaders all need to check that incoming data has the right shape before using it. In this problem you write the core of a small validation library: given a schema and a value, report every place where the value breaks the schema.
Implement validateSchema(schema, value). It returns an array of errors. An empty array means the value is valid.
validateSchema(schema, value) // => Array<{ path: string, code: string }>
A schema is a plain object with a type. Every node can also have optional: true, which only matters when the node is a property of an object schema.
{ type: 'string', minLength?, maxLength? }: a string. The limits apply to length.{ type: 'number', integer?, min?, max? }: a number. NaN is not a valid number. integer: true requires Number.isInteger(value).{ type: 'boolean' }: true or false.{ type: 'enum', values: [...] }: the value must equal (===) one of values.{ type: 'array', items, minItems?, maxItems? }: an array whose every element matches the schema items.{ type: 'object', properties, strict? }: any non-null object that is not an array. properties maps each key to a schema. With strict: true, keys that are not listed in properties are errors.All limits are inclusive: minLength: 2 accepts a string of length 2.
invalid_type: the value has the wrong type. null never matches, and an array never matches an object schema.required: a property without optional: true is missing, or its value is undefined.too_small / too_big: a string length, number, or array length is below the minimum or above the maximum.not_integer: the number is not an integer, but integer: true is set.invalid_enum: the value is not one of the enum’s values.unrecognized_key: a strict object has a key that is not in properties.An optional property may be missing or undefined. If it is present with any other value (including null), that value is validated normally.
'' (empty string).customer.email.lines[2], lines[2].sku, [0] for the first item of a root array.required or unrecognized_key error uses the path of the property itself.Errors are reported depth first, in this order:
invalid_type for it and do not check its limits or children.not_integer, then too_small, then too_big. Strings: too_small, then too_big.too_small / too_big first, then the errors of each item in index order.properties (not the value’s key order), then one unrecognized_key per extra key in Object.keys(value) order.const orderSchema = {
type: 'object',
strict: true,
properties: {
id: { type: 'string', minLength: 3 },
status: { type: 'enum', values: ['draft', 'paid', 'shipped'] },
customer: {
type: 'object',
properties: {
email: { type: 'string' },
vip: { type: 'boolean', optional: true },
},
},
lines: {
type: 'array',
minItems: 1,
items: {
type: 'object',
properties: {
sku: { type: 'string' },
qty: { type: 'number', integer: true, min: 1 },
},
},
},
},
};
validateSchema(orderSchema, {
id: 'ord_91',
status: 'paid',
customer: { email: 'mira@example.com' },
lines: [{ sku: 'TEA-01', qty: 2 }],
});
// => []
validateSchema(orderSchema, {
id: 'o1',
status: 'lost',
customer: { vip: 'yes' },
lines: [{ sku: 'TEA-01', qty: 0.5 }, { qty: 3 }],
coupon: 'SPRING',
});
// => [
// { path: 'id', code: 'too_small' },
// { path: 'status', code: 'invalid_enum' },
// { path: 'customer.email', code: 'required' },
// { path: 'customer.vip', code: 'invalid_type' },
// { path: 'lines[0].qty', code: 'not_integer' },
// { path: 'lines[0].qty', code: 'too_small' },
// { path: 'lines[1].sku', code: 'required' },
// { path: 'coupon', code: 'unrecognized_key' },
// ]
validateSchema({ type: 'number' }, NaN);
// => [{ path: '', code: 'invalid_type' }]
Object.keys).