|
| 1 | +import test from "ava"; |
| 2 | + |
| 3 | +import { setupTests } from "../testing-utils"; |
| 4 | + |
| 5 | +import * as json from "."; |
| 6 | + |
| 7 | +setupTests(test); |
| 8 | + |
| 9 | +const testSchema = { |
| 10 | + requiredKey: json.string, |
| 11 | +}; |
| 12 | + |
| 13 | +const optionalSchema = { |
| 14 | + optionalKey: json.optional(json.string), |
| 15 | +}; |
| 16 | + |
| 17 | +test("validateSchema - required properties are required", async (t) => { |
| 18 | + t.false(json.validateSchema(testSchema, {})); |
| 19 | + t.false(json.validateSchema(testSchema, { requiredKey: undefined })); |
| 20 | + t.false(json.validateSchema(testSchema, { requiredKey: null })); |
| 21 | + t.false(json.validateSchema(testSchema, { requiredKey: 0 })); |
| 22 | + t.false(json.validateSchema(testSchema, { requiredKey: 123 })); |
| 23 | + t.false(json.validateSchema(testSchema, { requiredKey: false })); |
| 24 | + t.false(json.validateSchema(testSchema, { requiredKey: true })); |
| 25 | + t.false(json.validateSchema(testSchema, { requiredKey: [] })); |
| 26 | + t.false(json.validateSchema(testSchema, { requiredKey: {} })); |
| 27 | + t.true(json.validateSchema(testSchema, { requiredKey: "" })); |
| 28 | + t.true(json.validateSchema(testSchema, { requiredKey: "foo" })); |
| 29 | +}); |
| 30 | + |
| 31 | +test("validateSchema - optional properties are optional", async (t) => { |
| 32 | + // Optional fields may be absent |
| 33 | + t.true(json.validateSchema(optionalSchema, {})); |
| 34 | + t.true(json.validateSchema(optionalSchema, { optionalKey: undefined })); |
| 35 | + t.true(json.validateSchema(optionalSchema, { optionalKey: null })); |
| 36 | + |
| 37 | + // But, if present, should have the expected type |
| 38 | + t.false(json.validateSchema(optionalSchema, { optionalKey: 0 })); |
| 39 | + t.false(json.validateSchema(optionalSchema, { optionalKey: 123 })); |
| 40 | + t.false(json.validateSchema(optionalSchema, { optionalKey: false })); |
| 41 | + t.false(json.validateSchema(optionalSchema, { optionalKey: true })); |
| 42 | + t.false(json.validateSchema(optionalSchema, { optionalKey: [] })); |
| 43 | + t.false(json.validateSchema(optionalSchema, { optionalKey: {} })); |
| 44 | + t.true(json.validateSchema(optionalSchema, { optionalKey: "" })); |
| 45 | + t.true(json.validateSchema(optionalSchema, { optionalKey: "foo" })); |
| 46 | +}); |
0 commit comments