Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
496 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | const Ajv = require("ajv")
|
---|
| 2 | const ajv = new Ajv({allErrors: true})
|
---|
| 3 |
|
---|
| 4 | const 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 |
|
---|
| 14 | const validate = ajv.compile(schema)
|
---|
| 15 |
|
---|
| 16 | test({foo: "abc", bar: 2})
|
---|
| 17 | test({foo: 2, bar: 4})
|
---|
| 18 |
|
---|
| 19 | function test(data) {
|
---|
| 20 | const valid = validate(data)
|
---|
| 21 | if (valid) console.log("Valid!")
|
---|
| 22 | else console.log("Invalid: " + ajv.errorsText(validate.errors))
|
---|
| 23 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.