.tonic_example.js 427 B

123456789101112131415161718192021
  1. const Ajv = require("ajv").default
  2. const ajv = new Ajv({allErrors: true})
  3. const schema = {
  4. properties: {
  5. foo: {type: "string"},
  6. bar: {type: "number", maximum: 3},
  7. },
  8. }
  9. const validate = ajv.compile(schema)
  10. test({foo: "abc", bar: 2})
  11. test({foo: 2, bar: 4})
  12. function test(data) {
  13. const valid = validate(data)
  14. if (valid) console.log("Valid!")
  15. else console.log("Invalid: " + ajv.errorsText(validate.errors))
  16. }