Model and schema validation done perfectly.
npm i -S @perfect-schema/base
import PerfectSchema from '@perfect-schema/base';
const itemSchema = new PerfectSchema({
_id: String,
name: {
type: String,
required: true,
minLength: 3
},
qty: {
type: PerfectSchema.Integer,
minInteger: 0,
defaultValue: 0
},
price: Number
});
const cartItemSchema = new PerfectSchema({
itemId: String,
qty: Number
});
const cartSchema = new PerfectSchema({
_id: String,
userId: String,
items: PerfectSchema.ArrayOf(cartItemSchema)
})
const context = cartSchema.createContext();
const item = cartSchema.createModel({
items: [{
name: 'foo'
}]
});
if (!context.validate(item)) {
// context.isValid() == false
console.error(context.getMessages());
}
MIT