|
| 1 | +import o from 'ospec'; |
| 2 | +import Ajv from 'ajv'; |
| 3 | +import { join } from 'path'; |
| 4 | +import { promises as fs } from 'fs'; |
| 5 | +import { AjvOptions, rootDirectory, schemaPath } from './validation.js'; |
| 6 | + |
| 7 | +const examplePath = join(rootDirectory, 'examples/item.json'); |
| 8 | + |
| 9 | +o.spec('Item', () => { |
| 10 | + let validate; |
| 11 | + const ajv = new Ajv(AjvOptions); |
| 12 | + |
| 13 | + o.before(async () => { |
| 14 | + const data = JSON.parse(await fs.readFile(schemaPath)); |
| 15 | + validate = await ajv.compileAsync(data); |
| 16 | + }); |
| 17 | + |
| 18 | + o('Example should pass validation', async () => { |
| 19 | + // given |
| 20 | + const example = JSON.parse(await fs.readFile(examplePath)); |
| 21 | + |
| 22 | + // when |
| 23 | + let valid = validate(example); |
| 24 | + |
| 25 | + // then |
| 26 | + o(valid).equals(true)(JSON.stringify(validate.errors, null, 2)); |
| 27 | + }); |
| 28 | + |
| 29 | + o("Example with invalid properties processing:software value should fail validation", async () => { |
| 30 | + // given |
| 31 | + const example = JSON.parse(await fs.readFile(examplePath)); |
| 32 | + example['properties']['processing:software'] = null; |
| 33 | + |
| 34 | + // when |
| 35 | + let valid = validate(example); |
| 36 | + |
| 37 | + // then |
| 38 | + o(valid).equals(false); |
| 39 | + o( |
| 40 | + validate.errors.some( |
| 41 | + (error) => |
| 42 | + error.instancePath === '/properties/processing:software' && |
| 43 | + error.message === 'must be object', |
| 44 | + ), |
| 45 | + ).equals(true)(JSON.stringify(validate.errors)); |
| 46 | + }); |
| 47 | + |
| 48 | + o("Example with invalid asset processing:software value should fail validation", async () => { |
| 49 | + // given |
| 50 | + const example = JSON.parse(await fs.readFile(examplePath)); |
| 51 | + example['assets']['manifest']['processing:software'] = null; |
| 52 | + |
| 53 | + // when |
| 54 | + let valid = validate(example); |
| 55 | + |
| 56 | + // then |
| 57 | + o(valid).equals(false); |
| 58 | + o( |
| 59 | + validate.errors.some( |
| 60 | + (error) => |
| 61 | + error.instancePath === '/assets/manifest/processing:software' && |
| 62 | + error.message === 'must be object', |
| 63 | + ), |
| 64 | + ).equals(true)(JSON.stringify(validate.errors)); |
| 65 | + }); |
| 66 | +}); |
0 commit comments