Skip to content

Commit 3f4cb18

Browse files
committed
fix: use draft errors instead of default
1 parent 3a8a3be commit 3f4cb18

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed

lib/features/anyOf.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* @draft-04
33
*/
44
import { mergeSchema } from "../mergeSchema";
5-
import errors from "../validation/errors";
65
import { JsonSchema, JsonPointer, JsonValidator, JsonError } from "../types";
76
import { Draft } from "../draft";
87
import { omit } from "../utils/omit";
@@ -45,7 +44,7 @@ export function resolveAnyOf(
4544

4645
const resolvedSchema = mergeValidAnyOfSchema(draft, schema, data);
4746
if (resolvedSchema == null) {
48-
return errors.anyOfError({ pointer, schema, value: data, anyOf: JSON.stringify(anyOf) });
47+
return draft.errors.anyOfError({ pointer, schema, value: data, anyOf: JSON.stringify(anyOf) });
4948
}
5049
const mergedSchema = mergeSchema(schema, resolvedSchema);
5150
return omit(mergedSchema, "anyOf");

lib/step.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import getTypeOf from "./getTypeOf";
22
import createSchemaOf from "./createSchemaOf";
3-
import errors from "./validation/errors";
43
import { JsonSchema, JsonPointer, JsonError, isJsonError } from "./types";
54
import { Draft } from "./draft";
65
import { reduceSchema } from "./reduceSchema";
@@ -33,7 +32,7 @@ const stepType: Record<string, StepFunction> = {
3332
}
3433
// @draft >= 7 bool schema, items:[true, false]
3534
if (schema.items[key] === false) {
36-
return errors.invalidDataError({
35+
return draft.errors.invalidDataError({
3736
key,
3837
value: itemValue,
3938
pointer,
@@ -46,7 +45,7 @@ const stepType: Record<string, StepFunction> = {
4645
}
4746

4847
if (schema.additionalItems === false) {
49-
return errors.additionalItemsError({
48+
return draft.errors.additionalItemsError({
5049
key,
5150
value: itemValue,
5251
pointer,
@@ -90,7 +89,7 @@ const stepType: Record<string, StepFunction> = {
9089

9190
// @feature boolean schema
9291
if (property === false) {
93-
return errors.forbiddenPropertyError({
92+
return draft.errors.forbiddenPropertyError({
9493
property: key,
9594
value: data,
9695
pointer,
@@ -141,7 +140,7 @@ const stepType: Record<string, StepFunction> = {
141140
return createSchemaOf(data[key]);
142141
}
143142

144-
return errors.unknownPropertyError({
143+
return draft.errors.unknownPropertyError({
145144
property: key,
146145
value: data,
147146
pointer: `${pointer}`,

lib/validation/format.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-disable max-len, no-control-regex */
2-
import errors from "./errors";
32
import { JsonError, JsonSchema } from "../types";
43
import { Draft } from "../draft";
54
import validUrl from "valid-url";
@@ -48,7 +47,7 @@ const formatValidators: Record<
4847
// full-date from http://tools.ietf.org/html/rfc3339#section-5.6
4948
const matches = value.match(matchDate);
5049
if (!matches) {
51-
return errors.formatDateTimeError({ value, pointer, schema });
50+
return draft.errors.formatDateTimeError({ value, pointer, schema });
5251
}
5352
const year = +matches[1];
5453
const month = +matches[2];
@@ -63,7 +62,7 @@ const formatValidators: Record<
6362
) {
6463
return undefined;
6564
}
66-
return errors.formatDateError({ value, pointer, schema });
65+
return draft.errors.formatDateError({ value, pointer, schema });
6766
},
6867

6968
"date-time": (draft, schema, value, pointer) => {
@@ -72,11 +71,11 @@ const formatValidators: Record<
7271
}
7372
if (value === "" || isValidDateTime.test(value)) {
7473
if (new Date(value).toString() === "Invalid Date") {
75-
return errors.formatDateTimeError({ value, pointer, schema });
74+
return draft.errors.formatDateTimeError({ value, pointer, schema });
7675
}
7776
return undefined;
7877
}
79-
return errors.formatDateTimeError({ value, pointer, schema });
78+
return draft.errors.formatDateTimeError({ value, pointer, schema });
8079
},
8180

8281
email: (draft, schema, value, pointer) => {
@@ -85,20 +84,20 @@ const formatValidators: Record<
8584
}
8685
// taken from https://github.com/ExodusMovement/schemasafe/blob/master/src/formats.js
8786
if (value[0] === '"') {
88-
return errors.formatEmailError({ value, pointer, schema });
87+
return draft.errors.formatEmailError({ value, pointer, schema });
8988
}
9089
const [name, host, ...rest] = value.split("@");
9190
if (!name || !host || rest.length !== 0 || name.length > 64 || host.length > 253) {
92-
return errors.formatEmailError({ value, pointer, schema });
91+
return draft.errors.formatEmailError({ value, pointer, schema });
9392
}
9493
if (name[0] === "." || name.endsWith(".") || name.includes("..")) {
95-
return errors.formatEmailError({ value, pointer, schema });
94+
return draft.errors.formatEmailError({ value, pointer, schema });
9695
}
9796
if (!/^[a-z0-9.-]+$/i.test(host) || !/^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+$/i.test(name)) {
98-
return errors.formatEmailError({ value, pointer, schema });
97+
return draft.errors.formatEmailError({ value, pointer, schema });
9998
}
10099
if (!host.split(".").every((part) => /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/i.test(part))) {
101-
return errors.formatEmailError({ value, pointer, schema });
100+
return draft.errors.formatEmailError({ value, pointer, schema });
102101
}
103102
return undefined;
104103
},
@@ -115,7 +114,7 @@ const formatValidators: Record<
115114
parseIdnEmail(value);
116115
return undefined;
117116
} catch (e) {
118-
return errors.formatEmailError({ value, pointer, schema });
117+
return draft.errors.formatEmailError({ value, pointer, schema });
119118
}
120119
},
121120

@@ -126,7 +125,7 @@ const formatValidators: Record<
126125
if (value === "" || isValidHostname.test(value)) {
127126
return undefined;
128127
}
129-
return errors.formatHostnameError({ value, pointer, schema });
128+
return draft.errors.formatHostnameError({ value, pointer, schema });
130129
},
131130

132131
ipv4: (draft, schema, value, pointer) => {
@@ -135,12 +134,12 @@ const formatValidators: Record<
135134
}
136135
if (value && value[0] === "0") {
137136
// leading zeroes should be rejected, as they are treated as octals
138-
return errors.formatIPV4LeadingZeroError({ value, pointer, schema });
137+
return draft.errors.formatIPV4LeadingZeroError({ value, pointer, schema });
139138
}
140139
if (value.length <= 15 && isValidIPV4.test(value)) {
141140
return undefined;
142141
}
143-
return errors.formatIPV4Error({ value, pointer, schema });
142+
return draft.errors.formatIPV4Error({ value, pointer, schema });
144143
},
145144

146145
ipv6: (draft, schema, value, pointer) => {
@@ -149,12 +148,12 @@ const formatValidators: Record<
149148
}
150149
if (value && value[0] === "0") {
151150
// leading zeroes should be rejected, as they are treated as octals
152-
return errors.formatIPV6LeadingZeroError({ value, pointer, schema });
151+
return draft.errors.formatIPV6LeadingZeroError({ value, pointer, schema });
153152
}
154153
if (value.length <= 45 && isValidIPV6.test(value)) {
155154
return undefined;
156155
}
157-
return errors.formatIPV6Error({ value, pointer, schema });
156+
return draft.errors.formatIPV6Error({ value, pointer, schema });
158157
},
159158

160159
"json-pointer": (draft, schema, value, pointer) => {
@@ -164,7 +163,7 @@ const formatValidators: Record<
164163
if (isValidJsonPointer.test(value)) {
165164
return undefined;
166165
}
167-
return errors.formatJsonPointerError({ value, pointer, schema });
166+
return draft.errors.formatJsonPointerError({ value, pointer, schema });
168167
},
169168

170169
"relative-json-pointer": (draft, schema, value, pointer) => {
@@ -174,7 +173,7 @@ const formatValidators: Record<
174173
if (isValidRelativeJsonPointer.test(value)) {
175174
return undefined;
176175
}
177-
return errors.formatJsonPointerError({ value, pointer, schema });
176+
return draft.errors.formatJsonPointerError({ value, pointer, schema });
178177
},
179178

180179
regex: (draft, schema, value, pointer) => {
@@ -184,13 +183,13 @@ const formatValidators: Record<
184183
return undefined;
185184
} catch (e) {} // eslint-disable-line no-empty
186185

187-
return errors.formatRegExError({ value, pointer, schema });
186+
return draft.errors.formatRegExError({ value, pointer, schema });
188187
}
189188
// v7 tests, ignore non-regex values
190189
if (typeof value === "object" || typeof value === "number" || Array.isArray(value)) {
191190
return undefined;
192191
}
193-
return errors.formatRegExError({ value, pointer, schema });
192+
return draft.errors.formatRegExError({ value, pointer, schema });
194193
},
195194

196195
// hh:mm:ss.sTZD
@@ -202,7 +201,7 @@ const formatValidators: Record<
202201
}
203202
// https://github.com/cfworker/cfworker/blob/main/packages/json-schema/src/format.ts
204203
const matches = value.match(matchTime);
205-
return matches ? undefined : errors.formatDateTimeError({ value, pointer, schema });
204+
return matches ? undefined : draft.errors.formatDateTimeError({ value, pointer, schema });
206205
// if (!matches) {
207206
// return errors.formatDateTimeError({ value, pointer, schema });
208207
// }
@@ -227,7 +226,7 @@ const formatValidators: Record<
227226
if (validUrl.isUri(value)) {
228227
return undefined;
229228
}
230-
return errors.formatURIError({ value, pointer, schema });
229+
return draft.errors.formatURIError({ value, pointer, schema });
231230
},
232231

233232
"uri-reference": (draft, schema, value, pointer) => {
@@ -237,7 +236,7 @@ const formatValidators: Record<
237236
if (isValidURIRef.test(value)) {
238237
return undefined;
239238
}
240-
return errors.formatURIReferenceError({ value, pointer, schema });
239+
return draft.errors.formatURIReferenceError({ value, pointer, schema });
241240
},
242241

243242
"uri-template": (draft, schema, value, pointer) => {
@@ -247,14 +246,14 @@ const formatValidators: Record<
247246
if (isValidURITemplate.test(value)) {
248247
return undefined;
249248
}
250-
return errors.formatURITemplateError({ value, pointer, schema });
249+
return draft.errors.formatURITemplateError({ value, pointer, schema });
251250
},
252251

253252
url: (draft, schema, value: string, pointer) => {
254253
if (value === "" || validUrl.isWebUri(value)) {
255254
return undefined;
256255
}
257-
return errors.formatURLError({ value, pointer, schema });
256+
return draft.errors.formatURLError({ value, pointer, schema });
258257
}
259258
};
260259

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-schema-library",
3-
"version": "9.1.1",
3+
"version": "9.1.2",
44
"description": "Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation",
55
"module": "dist/module/index.js",
66
"types": "dist/index.d.ts",

0 commit comments

Comments
 (0)