1 | /**
|
---|
2 | * @fileoverview The instance of Ajv validator.
|
---|
3 | * @author Evgeny Poberezkin
|
---|
4 | */
|
---|
5 |
|
---|
6 | //------------------------------------------------------------------------------
|
---|
7 | // Requirements
|
---|
8 | //------------------------------------------------------------------------------
|
---|
9 |
|
---|
10 | import Ajv from "ajv";
|
---|
11 |
|
---|
12 | //-----------------------------------------------------------------------------
|
---|
13 | // Helpers
|
---|
14 | //-----------------------------------------------------------------------------
|
---|
15 |
|
---|
16 | /*
|
---|
17 | * Copied from ajv/lib/refs/json-schema-draft-04.json
|
---|
18 | * The MIT License (MIT)
|
---|
19 | * Copyright (c) 2015-2017 Evgeny Poberezkin
|
---|
20 | */
|
---|
21 | const metaSchema = {
|
---|
22 | id: "http://json-schema.org/draft-04/schema#",
|
---|
23 | $schema: "http://json-schema.org/draft-04/schema#",
|
---|
24 | description: "Core schema meta-schema",
|
---|
25 | definitions: {
|
---|
26 | schemaArray: {
|
---|
27 | type: "array",
|
---|
28 | minItems: 1,
|
---|
29 | items: { $ref: "#" }
|
---|
30 | },
|
---|
31 | positiveInteger: {
|
---|
32 | type: "integer",
|
---|
33 | minimum: 0
|
---|
34 | },
|
---|
35 | positiveIntegerDefault0: {
|
---|
36 | allOf: [{ $ref: "#/definitions/positiveInteger" }, { default: 0 }]
|
---|
37 | },
|
---|
38 | simpleTypes: {
|
---|
39 | enum: ["array", "boolean", "integer", "null", "number", "object", "string"]
|
---|
40 | },
|
---|
41 | stringArray: {
|
---|
42 | type: "array",
|
---|
43 | items: { type: "string" },
|
---|
44 | minItems: 1,
|
---|
45 | uniqueItems: true
|
---|
46 | }
|
---|
47 | },
|
---|
48 | type: "object",
|
---|
49 | properties: {
|
---|
50 | id: {
|
---|
51 | type: "string"
|
---|
52 | },
|
---|
53 | $schema: {
|
---|
54 | type: "string"
|
---|
55 | },
|
---|
56 | title: {
|
---|
57 | type: "string"
|
---|
58 | },
|
---|
59 | description: {
|
---|
60 | type: "string"
|
---|
61 | },
|
---|
62 | default: { },
|
---|
63 | multipleOf: {
|
---|
64 | type: "number",
|
---|
65 | minimum: 0,
|
---|
66 | exclusiveMinimum: true
|
---|
67 | },
|
---|
68 | maximum: {
|
---|
69 | type: "number"
|
---|
70 | },
|
---|
71 | exclusiveMaximum: {
|
---|
72 | type: "boolean",
|
---|
73 | default: false
|
---|
74 | },
|
---|
75 | minimum: {
|
---|
76 | type: "number"
|
---|
77 | },
|
---|
78 | exclusiveMinimum: {
|
---|
79 | type: "boolean",
|
---|
80 | default: false
|
---|
81 | },
|
---|
82 | maxLength: { $ref: "#/definitions/positiveInteger" },
|
---|
83 | minLength: { $ref: "#/definitions/positiveIntegerDefault0" },
|
---|
84 | pattern: {
|
---|
85 | type: "string",
|
---|
86 | format: "regex"
|
---|
87 | },
|
---|
88 | additionalItems: {
|
---|
89 | anyOf: [
|
---|
90 | { type: "boolean" },
|
---|
91 | { $ref: "#" }
|
---|
92 | ],
|
---|
93 | default: { }
|
---|
94 | },
|
---|
95 | items: {
|
---|
96 | anyOf: [
|
---|
97 | { $ref: "#" },
|
---|
98 | { $ref: "#/definitions/schemaArray" }
|
---|
99 | ],
|
---|
100 | default: { }
|
---|
101 | },
|
---|
102 | maxItems: { $ref: "#/definitions/positiveInteger" },
|
---|
103 | minItems: { $ref: "#/definitions/positiveIntegerDefault0" },
|
---|
104 | uniqueItems: {
|
---|
105 | type: "boolean",
|
---|
106 | default: false
|
---|
107 | },
|
---|
108 | maxProperties: { $ref: "#/definitions/positiveInteger" },
|
---|
109 | minProperties: { $ref: "#/definitions/positiveIntegerDefault0" },
|
---|
110 | required: { $ref: "#/definitions/stringArray" },
|
---|
111 | additionalProperties: {
|
---|
112 | anyOf: [
|
---|
113 | { type: "boolean" },
|
---|
114 | { $ref: "#" }
|
---|
115 | ],
|
---|
116 | default: { }
|
---|
117 | },
|
---|
118 | definitions: {
|
---|
119 | type: "object",
|
---|
120 | additionalProperties: { $ref: "#" },
|
---|
121 | default: { }
|
---|
122 | },
|
---|
123 | properties: {
|
---|
124 | type: "object",
|
---|
125 | additionalProperties: { $ref: "#" },
|
---|
126 | default: { }
|
---|
127 | },
|
---|
128 | patternProperties: {
|
---|
129 | type: "object",
|
---|
130 | additionalProperties: { $ref: "#" },
|
---|
131 | default: { }
|
---|
132 | },
|
---|
133 | dependencies: {
|
---|
134 | type: "object",
|
---|
135 | additionalProperties: {
|
---|
136 | anyOf: [
|
---|
137 | { $ref: "#" },
|
---|
138 | { $ref: "#/definitions/stringArray" }
|
---|
139 | ]
|
---|
140 | }
|
---|
141 | },
|
---|
142 | enum: {
|
---|
143 | type: "array",
|
---|
144 | minItems: 1,
|
---|
145 | uniqueItems: true
|
---|
146 | },
|
---|
147 | type: {
|
---|
148 | anyOf: [
|
---|
149 | { $ref: "#/definitions/simpleTypes" },
|
---|
150 | {
|
---|
151 | type: "array",
|
---|
152 | items: { $ref: "#/definitions/simpleTypes" },
|
---|
153 | minItems: 1,
|
---|
154 | uniqueItems: true
|
---|
155 | }
|
---|
156 | ]
|
---|
157 | },
|
---|
158 | format: { type: "string" },
|
---|
159 | allOf: { $ref: "#/definitions/schemaArray" },
|
---|
160 | anyOf: { $ref: "#/definitions/schemaArray" },
|
---|
161 | oneOf: { $ref: "#/definitions/schemaArray" },
|
---|
162 | not: { $ref: "#" }
|
---|
163 | },
|
---|
164 | dependencies: {
|
---|
165 | exclusiveMaximum: ["maximum"],
|
---|
166 | exclusiveMinimum: ["minimum"]
|
---|
167 | },
|
---|
168 | default: { }
|
---|
169 | };
|
---|
170 |
|
---|
171 | //------------------------------------------------------------------------------
|
---|
172 | // Public Interface
|
---|
173 | //------------------------------------------------------------------------------
|
---|
174 |
|
---|
175 | export default (additionalOptions = {}) => {
|
---|
176 | const ajv = new Ajv({
|
---|
177 | meta: false,
|
---|
178 | useDefaults: true,
|
---|
179 | validateSchema: false,
|
---|
180 | missingRefs: "ignore",
|
---|
181 | verbose: true,
|
---|
182 | schemaId: "auto",
|
---|
183 | ...additionalOptions
|
---|
184 | });
|
---|
185 |
|
---|
186 | ajv.addMetaSchema(metaSchema);
|
---|
187 | // eslint-disable-next-line no-underscore-dangle
|
---|
188 | ajv._opts.defaultMeta = metaSchema.id;
|
---|
189 |
|
---|
190 | return ajv;
|
---|
191 | };
|
---|