fast-json-stringify 5.0.5 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/validator.js ADDED
@@ -0,0 +1,75 @@
1
+ 'use strict'
2
+
3
+ const Ajv = require('ajv')
4
+ const fastUri = require('fast-uri')
5
+ const ajvFormats = require('ajv-formats')
6
+ const clone = require('rfdc')({ proto: true })
7
+
8
+ class Validator {
9
+ constructor (ajvOptions) {
10
+ this.ajv = new Ajv({
11
+ ...ajvOptions,
12
+ strictSchema: false,
13
+ validateSchema: false,
14
+ allowUnionTypes: true,
15
+ uriResolver: fastUri
16
+ })
17
+
18
+ ajvFormats(this.ajv)
19
+
20
+ this.ajv.addKeyword({
21
+ keyword: 'fjs_type',
22
+ type: 'object',
23
+ errors: false,
24
+ validate: (type, date) => {
25
+ return date instanceof Date
26
+ }
27
+ })
28
+ }
29
+
30
+ addSchema (schema, schemaName) {
31
+ let schemaKey = schema.$id || schemaName
32
+ if (schema.$id !== undefined && schema.$id[0] === '#') {
33
+ schemaKey = schemaName + schema.$id // relative URI
34
+ }
35
+
36
+ if (
37
+ this.ajv.refs[schemaKey] === undefined &&
38
+ this.ajv.schemas[schemaKey] === undefined
39
+ ) {
40
+ const ajvSchema = clone(schema)
41
+ this.convertSchemaToAjvFormat(ajvSchema)
42
+ this.ajv.addSchema(ajvSchema, schemaKey)
43
+ }
44
+ }
45
+
46
+ validate (schemaRef, data) {
47
+ return this.ajv.validate(schemaRef, data)
48
+ }
49
+
50
+ // Ajv does not support js date format. In order to properly validate objects containing a date,
51
+ // it needs to replace all occurrences of the string date format with a custom keyword fjs_type.
52
+ // (see https://github.com/fastify/fast-json-stringify/pull/441)
53
+ convertSchemaToAjvFormat (schema) {
54
+ if (schema === null) return
55
+
56
+ if (schema.type === 'string') {
57
+ schema.fjs_type = 'string'
58
+ schema.type = ['string', 'object']
59
+ } else if (
60
+ Array.isArray(schema.type) &&
61
+ schema.type.includes('string') &&
62
+ !schema.type.includes('object')
63
+ ) {
64
+ schema.fjs_type = 'string'
65
+ schema.type.push('object')
66
+ }
67
+ for (const property in schema) {
68
+ if (typeof schema[property] === 'object') {
69
+ this.convertSchemaToAjvFormat(schema[property])
70
+ }
71
+ }
72
+ }
73
+ }
74
+
75
+ module.exports = Validator
package/ajv.js DELETED
@@ -1,37 +0,0 @@
1
- 'use strict'
2
-
3
- const Ajv = require('ajv')
4
- const fastUri = require('fast-uri')
5
- const ajvFormats = require('ajv-formats')
6
-
7
- module.exports = buildAjv
8
-
9
- function buildAjv (options) {
10
- const ajvInstance = new Ajv({ ...options, strictSchema: false, validateSchema: false, uriResolver: fastUri })
11
- ajvFormats(ajvInstance)
12
-
13
- const validateDateTimeFormat = ajvFormats.get('date-time').validate
14
- const validateDateFormat = ajvFormats.get('date').validate
15
- const validateTimeFormat = ajvFormats.get('time').validate
16
-
17
- ajvInstance.addKeyword({
18
- keyword: 'fjs_date_type',
19
- validate: (schema, date) => {
20
- if (date instanceof Date) {
21
- return true
22
- }
23
- if (schema === 'date-time') {
24
- return validateDateTimeFormat(date)
25
- }
26
- if (schema === 'date') {
27
- return validateDateFormat(date)
28
- }
29
- if (schema === 'time') {
30
- return validateTimeFormat(date)
31
- }
32
- return false
33
- }
34
- })
35
-
36
- return ajvInstance
37
- }