forked from fastify/fast-json-stringify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajv.js
37 lines (31 loc) · 939 Bytes
/
ajv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict'
const Ajv = require('ajv')
const fastUri = require('fast-uri')
const ajvFormats = require('ajv-formats')
module.exports = buildAjv
function buildAjv (options) {
const ajvInstance = new Ajv({ ...options, strictSchema: false, uriResolver: fastUri })
ajvFormats(ajvInstance)
const validateDateTimeFormat = ajvFormats.get('date-time').validate
const validateDateFormat = ajvFormats.get('date').validate
const validateTimeFormat = ajvFormats.get('time').validate
ajvInstance.addKeyword({
keyword: 'fjs_date_type',
validate: (schema, date) => {
if (date instanceof Date) {
return true
}
if (schema === 'date-time') {
return validateDateTimeFormat(date)
}
if (schema === 'date') {
return validateDateFormat(date)
}
if (schema === 'time') {
return validateTimeFormat(date)
}
return false
}
})
return ajvInstance
}