Urara-Blog/node_modules/.pnpm-store/v3/files/b6/46fcc8fd02a8063261739c1b1152fb52f91885e2e33e26bbb94ae5452fa0ce270149a9d5276852eac70e85dcbfeb0644f7458f18a07424583d83ffd2f18705
2022-08-14 01:14:53 +08:00

23 lines
496 B
Text

const Ajv = require("ajv")
const ajv = new Ajv({allErrors: true})
const schema = {
type: "object",
properties: {
foo: {type: "string"},
bar: {type: "number", maximum: 3},
},
required: ["foo", "bar"],
additionalProperties: false,
}
const validate = ajv.compile(schema)
test({foo: "abc", bar: 2})
test({foo: 2, bar: 4})
function test(data) {
const valid = validate(data)
if (valid) console.log("Valid!")
else console.log("Invalid: " + ajv.errorsText(validate.errors))
}