UNPKG

496 BJavaScriptView Raw
1const Ajv = require("ajv")
2const ajv = new Ajv({allErrors: true})
3
4const schema = {
5 type: "object",
6 properties: {
7 foo: {type: "string"},
8 bar: {type: "number", maximum: 3},
9 },
10 required: ["foo", "bar"],
11 additionalProperties: false,
12}
13
14const validate = ajv.compile(schema)
15
16test({foo: "abc", bar: 2})
17test({foo: 2, bar: 4})
18
19function test(data) {
20 const valid = validate(data)
21 if (valid) console.log("Valid!")
22 else console.log("Invalid: " + ajv.errorsText(validate.errors))
23}