|
1 | | -var test = require("tape"); |
2 | | -var Validator = require("jsonschema").Validator; |
| 1 | +'use strict' |
| 2 | +const test = require('tape') |
| 3 | +const Ajv = require('ajv') |
3 | 4 |
|
4 | | -const fixtures = require("./__test__/dates.json"); |
5 | | -// var mockDateSchema = require('./__test__/mockDateSchema.json'); |
| 5 | +const schemaResume = require('@jsonresume/schema/schemas/resume.json') |
6 | 6 |
|
7 | | -const mockDateSchema = { |
8 | | - type: "string", |
9 | | - description: "Mock Date Format", |
10 | | - pattern: |
11 | | - "^([1-2][0-9]{3}-[0-1][0-9]-[0-3][0-9]|[1-2][0-9]{3}-[0-1][0-9]|[1-2][0-9]{3})$", |
12 | | -}; |
| 7 | +const fixtures = require('./__test__/dates.json'); |
| 8 | + |
| 9 | +const miniDateSchema = { |
| 10 | + type: 'string', |
| 11 | + description: 'Mock Date Format', |
13 | 12 |
|
14 | | -function dateValidate(resumeJson, callback) { |
15 | | - var v = new Validator(); |
| 13 | + // copy 'date' pattern from resume schema |
| 14 | + pattern: schemaResume['$defs']['iso8601']['pattern'], |
| 15 | +}; |
16 | 16 |
|
17 | | - const validation = v.validate(resumeJson, mockDateSchema); |
| 17 | +function dateValidate (resumeJson, callback) { |
| 18 | + const ajv = new Ajv(); |
| 19 | + const validate = ajv.compile(miniDateSchema) |
18 | 20 |
|
19 | | - if (!validation.valid) { |
20 | | - return callback(validation.errors, false); |
| 21 | + if (!validate(resumeJson)) { |
| 22 | + return callback(validate.errors, false); |
21 | 23 | } |
22 | 24 |
|
23 | 25 | return callback(null, true); |
24 | 26 | } |
25 | 27 |
|
26 | | -test("dates - YYYY-MM-DD", (t) => { |
| 28 | +test('dates - YYYY-MM-DD', (t) => { |
27 | 29 | dateValidate(fixtures.yearMonthDay, (err, valid) => { |
28 | | - t.equal(err, null, "err should be null"); |
29 | | - t.true(valid, "valid is true"); |
| 30 | + t.equal(err, null, 'err should be null'); |
| 31 | + t.true(valid, 'valid is true'); |
30 | 32 | }); |
31 | 33 | t.end(); |
32 | 34 | }); |
33 | 35 |
|
34 | | -test("dates - YYYY-MM", (t) => { |
| 36 | +test('dates - YYYY-MM', (t) => { |
35 | 37 | dateValidate(fixtures.yearMonth, (err, valid) => { |
36 | | - t.equal(err, null, "err should be null"); |
37 | | - t.true(valid, "valid is true"); |
| 38 | + t.equal(err, null, 'err should be null'); |
| 39 | + t.true(valid, 'valid is true'); |
38 | 40 | }); |
39 | 41 | t.end(); |
40 | 42 | }); |
41 | 43 |
|
42 | | -test("dates - YYYY", (t) => { |
| 44 | +test('dates - YYYY', (t) => { |
43 | 45 | dateValidate(fixtures.yearMonthDay, (err, valid) => { |
44 | | - t.equal(err, null, "err should be null"); |
45 | | - t.true(valid, "valid is true"); |
| 46 | + t.equal(err, null, 'err should be null'); |
| 47 | + t.true(valid, 'valid is true'); |
46 | 48 | }); |
47 | 49 | t.end(); |
48 | 50 | }); |
49 | 51 |
|
50 | | -test("dates - invalid", (t) => { |
| 52 | +test('dates - invalid', (t) => { |
51 | 53 | dateValidate(fixtures.invalid, (err, valid) => { |
52 | | - t.notEqual(err, null, "err should contain an error"); |
53 | | - t.false(valid, "valid is false"); |
| 54 | + t.notEqual(err, null, 'err should contain an error'); |
| 55 | + t.false(valid, 'valid is false'); |
54 | 56 | }); |
55 | 57 | t.end(); |
56 | 58 | }); |
0 commit comments