@aeriajs/validation 0.0.57 → 0.0.58
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/dist/validate.d.ts +3 -5
- package/dist/validate.js +27 -31
- package/dist/validate.mjs +27 -27
- package/package.json +3 -3
package/dist/validate.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Either, JsonSchema, Property, InferSchema, Description, PropertyValidationError, ValidationError } from '@aeriajs/types';
|
|
2
|
-
import {
|
|
2
|
+
import { ValidationErrorCode } from '@aeriajs/types';
|
|
3
3
|
export type ValidateOptions = {
|
|
4
4
|
extraneous?: string[] | boolean;
|
|
5
5
|
filterOutExtraneous?: boolean;
|
|
@@ -7,9 +7,9 @@ export type ValidateOptions = {
|
|
|
7
7
|
coerce?: boolean;
|
|
8
8
|
};
|
|
9
9
|
export declare const makeValidationError: <TValidationError extends ValidationError>(error: TValidationError) => TValidationError;
|
|
10
|
-
export declare const validateProperty: (propName: string, what:
|
|
10
|
+
export declare const validateProperty: <TWhat>(propName: string, what: TWhat, property: Property | undefined, options?: ValidateOptions) => Either<PropertyValidationError | ValidationError, any>;
|
|
11
11
|
export declare const validateWholeness: (what: Record<string, any>, schema: Omit<JsonSchema, '$id'>) => {
|
|
12
|
-
code:
|
|
12
|
+
code: ValidationErrorCode.MissingProperties;
|
|
13
13
|
errors: {
|
|
14
14
|
[k: string]: {
|
|
15
15
|
type: "missing";
|
|
@@ -17,6 +17,4 @@ export declare const validateWholeness: (what: Record<string, any>, schema: Omit
|
|
|
17
17
|
};
|
|
18
18
|
} | undefined;
|
|
19
19
|
export declare const validate: <TWhat, const TJsonSchema extends Property | Omit<Description, "$id">>(what: TWhat | undefined, schema: TJsonSchema, options?: ValidateOptions) => import("@aeriajs/types").Left<PropertyValidationError | ValidationError> | import("@aeriajs/types").Right<InferSchema<TJsonSchema>>;
|
|
20
|
-
export declare const validateSilently: <TWhat, const TJsonSchema extends Property | Omit<Description, "$id">>(what: TWhat | undefined, schema: TJsonSchema, options?: ValidateOptions) => InferSchema<TJsonSchema> | null;
|
|
21
20
|
export declare const validator: <const TJsonSchema extends Property | Omit<Description, "$id">>(schema: TJsonSchema, options?: ValidateOptions) => readonly [InferSchema<TJsonSchema>, <TWhat>(what: TWhat) => import("@aeriajs/types").Left<PropertyValidationError | ValidationError> | import("@aeriajs/types").Right<InferSchema<TJsonSchema>>];
|
|
22
|
-
export declare const silentValidator: <const TJsonSchema extends Property | Omit<Description, "$id">>(schema: TJsonSchema, options?: ValidateOptions) => readonly [InferSchema<TJsonSchema>, <TWhat>(what: TWhat) => InferSchema<TJsonSchema> | null];
|
package/dist/validate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.validator = exports.validate = exports.validateWholeness = exports.validateProperty = exports.makeValidationError = void 0;
|
|
4
4
|
const common_1 = require("@aeriajs/common");
|
|
5
5
|
const types_1 = require("@aeriajs/types");
|
|
6
6
|
const getValueType = (value) => {
|
|
@@ -51,7 +51,7 @@ const validateProperty = (propName, what, property, options = {}) => {
|
|
|
51
51
|
}
|
|
52
52
|
return (0, common_1.right)(what);
|
|
53
53
|
}
|
|
54
|
-
return (0, common_1.left)(makePropertyError(
|
|
54
|
+
return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.Extraneous, {
|
|
55
55
|
expected: 'undefined',
|
|
56
56
|
got: getValueType(what),
|
|
57
57
|
}));
|
|
@@ -64,7 +64,7 @@ const validateProperty = (propName, what, property, options = {}) => {
|
|
|
64
64
|
}
|
|
65
65
|
if ('const' in property) {
|
|
66
66
|
if (what !== property.const) {
|
|
67
|
-
return (0, common_1.left)(makePropertyError(
|
|
67
|
+
return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
|
|
68
68
|
expected: property.const,
|
|
69
69
|
got: what,
|
|
70
70
|
}));
|
|
@@ -85,34 +85,40 @@ const validateProperty = (propName, what, property, options = {}) => {
|
|
|
85
85
|
if (expectedType === 'boolean' && !what) {
|
|
86
86
|
return (0, common_1.right)(what);
|
|
87
87
|
}
|
|
88
|
-
if ('$ref' in property &&
|
|
88
|
+
if ('$ref' in property && typeof what === 'string') {
|
|
89
89
|
if (/^[0-9a-fA-F]{24}$/.test(what)) {
|
|
90
90
|
return (0, common_1.right)(what);
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
if (coerce) {
|
|
94
|
-
if (expectedType === 'number' &&
|
|
94
|
+
if (expectedType === 'number' && typeof what === 'string') {
|
|
95
95
|
const coerced = parseFloat(what);
|
|
96
96
|
if (!isNaN(coerced)) {
|
|
97
97
|
return (0, common_1.right)(coerced);
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
if (expectedType === 'integer' &&
|
|
100
|
+
if (expectedType === 'integer' && typeof what === 'string') {
|
|
101
101
|
const coerced = parseInt(what);
|
|
102
102
|
if (!isNaN(coerced)) {
|
|
103
103
|
return (0, common_1.right)(coerced);
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
|
-
if (expectedType === 'string' &&
|
|
106
|
+
if (expectedType === 'string' && typeof what === 'number') {
|
|
107
107
|
return (0, common_1.right)(String(what));
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
return (0, common_1.left)(makePropertyError(
|
|
110
|
+
return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
|
|
111
111
|
expected: expectedType,
|
|
112
112
|
got: actualType,
|
|
113
113
|
}));
|
|
114
114
|
}
|
|
115
115
|
if ('items' in property) {
|
|
116
|
+
if (!Array.isArray(what)) {
|
|
117
|
+
return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
|
|
118
|
+
expected: expectedType,
|
|
119
|
+
got: actualType,
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
116
122
|
let i = 0;
|
|
117
123
|
for (const elem of what) {
|
|
118
124
|
const resultEither = (0, exports.validateProperty)(propName, elem, property.items, options);
|
|
@@ -130,18 +136,24 @@ const validateProperty = (propName, what, property, options = {}) => {
|
|
|
130
136
|
else if ('type' in property) {
|
|
131
137
|
if (property.type === 'integer') {
|
|
132
138
|
if (!Number.isInteger(what)) {
|
|
133
|
-
return (0, common_1.left)(makePropertyError(
|
|
139
|
+
return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
|
|
134
140
|
expected: 'integer',
|
|
135
141
|
got: 'invalid_number',
|
|
136
142
|
}));
|
|
137
143
|
}
|
|
138
144
|
}
|
|
139
145
|
if (property.type === 'integer' || property.type === 'number') {
|
|
146
|
+
if (typeof what !== 'number') {
|
|
147
|
+
return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
|
|
148
|
+
expected: expectedType,
|
|
149
|
+
got: actualType,
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
140
152
|
if ((property.maximum && property.maximum < what)
|
|
141
153
|
|| (property.minimum && property.minimum > what)
|
|
142
154
|
|| (property.exclusiveMaximum && property.exclusiveMaximum <= what)
|
|
143
155
|
|| (property.exclusiveMinimum && property.exclusiveMinimum >= what)) {
|
|
144
|
-
return (0, common_1.left)(makePropertyError(
|
|
156
|
+
return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
|
|
145
157
|
expected: 'number',
|
|
146
158
|
got: 'invalid_number',
|
|
147
159
|
}));
|
|
@@ -150,7 +162,7 @@ const validateProperty = (propName, what, property, options = {}) => {
|
|
|
150
162
|
}
|
|
151
163
|
else if ('enum' in property) {
|
|
152
164
|
if (!property.enum.includes(what)) {
|
|
153
|
-
return (0, common_1.left)(makePropertyError(
|
|
165
|
+
return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.ExtraneousElement, {
|
|
154
166
|
expected: property.enum,
|
|
155
167
|
got: what,
|
|
156
168
|
}));
|
|
@@ -166,7 +178,7 @@ const validateWholeness = (what, schema) => {
|
|
|
166
178
|
const missingProps = (0, common_1.getMissingProperties)(what, schema, required);
|
|
167
179
|
if (missingProps.length > 0) {
|
|
168
180
|
return (0, exports.makeValidationError)({
|
|
169
|
-
code: types_1.
|
|
181
|
+
code: types_1.ValidationErrorCode.MissingProperties,
|
|
170
182
|
errors: Object.fromEntries(missingProps
|
|
171
183
|
.map((error) => [
|
|
172
184
|
error,
|
|
@@ -181,7 +193,7 @@ exports.validateWholeness = validateWholeness;
|
|
|
181
193
|
const validate = (what, schema, options = {}) => {
|
|
182
194
|
if (!what) {
|
|
183
195
|
return (0, common_1.left)((0, exports.makeValidationError)({
|
|
184
|
-
code: types_1.
|
|
196
|
+
code: types_1.ValidationErrorCode.EmptyTarget,
|
|
185
197
|
errors: {},
|
|
186
198
|
}));
|
|
187
199
|
}
|
|
@@ -210,27 +222,20 @@ const validate = (what, schema, options = {}) => {
|
|
|
210
222
|
}
|
|
211
223
|
if (Object.keys(errors).length > 0) {
|
|
212
224
|
if (options.throwOnError) {
|
|
213
|
-
const error = new TypeError(types_1.
|
|
225
|
+
const error = new TypeError(types_1.ValidationErrorCode.InvalidProperties);
|
|
214
226
|
Object.assign(error, {
|
|
215
227
|
errors,
|
|
216
228
|
});
|
|
217
229
|
throw error;
|
|
218
230
|
}
|
|
219
231
|
return (0, common_1.left)((0, exports.makeValidationError)({
|
|
220
|
-
code: types_1.
|
|
232
|
+
code: types_1.ValidationErrorCode.InvalidProperties,
|
|
221
233
|
errors,
|
|
222
234
|
}));
|
|
223
235
|
}
|
|
224
236
|
return (0, common_1.right)(resultCopy);
|
|
225
237
|
};
|
|
226
238
|
exports.validate = validate;
|
|
227
|
-
const validateSilently = (what, schema, options = {}) => {
|
|
228
|
-
const resultEither = (0, exports.validate)(what, schema, options);
|
|
229
|
-
return (0, common_1.isLeft)(resultEither)
|
|
230
|
-
? null
|
|
231
|
-
: (0, common_1.unwrapEither)(resultEither);
|
|
232
|
-
};
|
|
233
|
-
exports.validateSilently = validateSilently;
|
|
234
239
|
const validator = (schema, options = {}) => {
|
|
235
240
|
return [
|
|
236
241
|
{},
|
|
@@ -240,12 +245,3 @@ const validator = (schema, options = {}) => {
|
|
|
240
245
|
];
|
|
241
246
|
};
|
|
242
247
|
exports.validator = validator;
|
|
243
|
-
const silentValidator = (schema, options = {}) => {
|
|
244
|
-
return [
|
|
245
|
-
{},
|
|
246
|
-
(what) => {
|
|
247
|
-
return (0, exports.validateSilently)(what, schema, options);
|
|
248
|
-
},
|
|
249
|
-
];
|
|
250
|
-
};
|
|
251
|
-
exports.silentValidator = silentValidator;
|
package/dist/validate.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { isLeft, left, right, unwrapEither, getMissingProperties } from "@aeriajs/common";
|
|
3
|
-
import {
|
|
3
|
+
import { ValidationErrorCode, PropertyValidationErrorCode } from "@aeriajs/types";
|
|
4
4
|
const getValueType = (value) => {
|
|
5
5
|
return Array.isArray(value) ? "array" : typeof value;
|
|
6
6
|
};
|
|
@@ -44,7 +44,7 @@ export const validateProperty = (propName, what, property, options = {}) => {
|
|
|
44
44
|
}
|
|
45
45
|
return right(what);
|
|
46
46
|
}
|
|
47
|
-
return left(makePropertyError(
|
|
47
|
+
return left(makePropertyError(PropertyValidationErrorCode.Extraneous, {
|
|
48
48
|
expected: "undefined",
|
|
49
49
|
got: getValueType(what)
|
|
50
50
|
}));
|
|
@@ -57,7 +57,7 @@ export const validateProperty = (propName, what, property, options = {}) => {
|
|
|
57
57
|
}
|
|
58
58
|
if ("const" in property) {
|
|
59
59
|
if (what !== property.const) {
|
|
60
|
-
return left(makePropertyError(
|
|
60
|
+
return left(makePropertyError(PropertyValidationErrorCode.Unmatching, {
|
|
61
61
|
expected: property.const,
|
|
62
62
|
got: what
|
|
63
63
|
}));
|
|
@@ -76,34 +76,40 @@ export const validateProperty = (propName, what, property, options = {}) => {
|
|
|
76
76
|
if (expectedType === "boolean" && !what) {
|
|
77
77
|
return right(what);
|
|
78
78
|
}
|
|
79
|
-
if ("$ref" in property &&
|
|
79
|
+
if ("$ref" in property && typeof what === "string") {
|
|
80
80
|
if (/^[0-9a-fA-F]{24}$/.test(what)) {
|
|
81
81
|
return right(what);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
if (coerce) {
|
|
85
|
-
if (expectedType === "number" &&
|
|
85
|
+
if (expectedType === "number" && typeof what === "string") {
|
|
86
86
|
const coerced = parseFloat(what);
|
|
87
87
|
if (!isNaN(coerced)) {
|
|
88
88
|
return right(coerced);
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
-
if (expectedType === "integer" &&
|
|
91
|
+
if (expectedType === "integer" && typeof what === "string") {
|
|
92
92
|
const coerced = parseInt(what);
|
|
93
93
|
if (!isNaN(coerced)) {
|
|
94
94
|
return right(coerced);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
if (expectedType === "string" &&
|
|
97
|
+
if (expectedType === "string" && typeof what === "number") {
|
|
98
98
|
return right(String(what));
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
-
return left(makePropertyError(
|
|
101
|
+
return left(makePropertyError(PropertyValidationErrorCode.Unmatching, {
|
|
102
102
|
expected: expectedType,
|
|
103
103
|
got: actualType
|
|
104
104
|
}));
|
|
105
105
|
}
|
|
106
106
|
if ("items" in property) {
|
|
107
|
+
if (!Array.isArray(what)) {
|
|
108
|
+
return left(makePropertyError(PropertyValidationErrorCode.Unmatching, {
|
|
109
|
+
expected: expectedType,
|
|
110
|
+
got: actualType
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
107
113
|
let i = 0;
|
|
108
114
|
for (const elem of what) {
|
|
109
115
|
const resultEither = validateProperty(propName, elem, property.items, options);
|
|
@@ -120,15 +126,21 @@ export const validateProperty = (propName, what, property, options = {}) => {
|
|
|
120
126
|
} else if ("type" in property) {
|
|
121
127
|
if (property.type === "integer") {
|
|
122
128
|
if (!Number.isInteger(what)) {
|
|
123
|
-
return left(makePropertyError(
|
|
129
|
+
return left(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
|
|
124
130
|
expected: "integer",
|
|
125
131
|
got: "invalid_number"
|
|
126
132
|
}));
|
|
127
133
|
}
|
|
128
134
|
}
|
|
129
135
|
if (property.type === "integer" || property.type === "number") {
|
|
136
|
+
if (typeof what !== "number") {
|
|
137
|
+
return left(makePropertyError(PropertyValidationErrorCode.Unmatching, {
|
|
138
|
+
expected: expectedType,
|
|
139
|
+
got: actualType
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
130
142
|
if (property.maximum && property.maximum < what || property.minimum && property.minimum > what || property.exclusiveMaximum && property.exclusiveMaximum <= what || property.exclusiveMinimum && property.exclusiveMinimum >= what) {
|
|
131
|
-
return left(makePropertyError(
|
|
143
|
+
return left(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
|
|
132
144
|
expected: "number",
|
|
133
145
|
got: "invalid_number"
|
|
134
146
|
}));
|
|
@@ -136,7 +148,7 @@ export const validateProperty = (propName, what, property, options = {}) => {
|
|
|
136
148
|
}
|
|
137
149
|
} else if ("enum" in property) {
|
|
138
150
|
if (!property.enum.includes(what)) {
|
|
139
|
-
return left(makePropertyError(
|
|
151
|
+
return left(makePropertyError(PropertyValidationErrorCode.ExtraneousElement, {
|
|
140
152
|
expected: property.enum,
|
|
141
153
|
got: what
|
|
142
154
|
}));
|
|
@@ -149,7 +161,7 @@ export const validateWholeness = (what, schema) => {
|
|
|
149
161
|
const missingProps = getMissingProperties(what, schema, required);
|
|
150
162
|
if (missingProps.length > 0) {
|
|
151
163
|
return makeValidationError({
|
|
152
|
-
code:
|
|
164
|
+
code: ValidationErrorCode.MissingProperties,
|
|
153
165
|
errors: Object.fromEntries(missingProps.map((error) => [
|
|
154
166
|
error,
|
|
155
167
|
{
|
|
@@ -162,7 +174,7 @@ export const validateWholeness = (what, schema) => {
|
|
|
162
174
|
export const validate = (what, schema, options = {}) => {
|
|
163
175
|
if (!what) {
|
|
164
176
|
return left(makeValidationError({
|
|
165
|
-
code:
|
|
177
|
+
code: ValidationErrorCode.EmptyTarget,
|
|
166
178
|
errors: {}
|
|
167
179
|
}));
|
|
168
180
|
}
|
|
@@ -194,23 +206,19 @@ export const validate = (what, schema, options = {}) => {
|
|
|
194
206
|
}
|
|
195
207
|
if (Object.keys(errors).length > 0) {
|
|
196
208
|
if (options.throwOnError) {
|
|
197
|
-
const error = new TypeError(
|
|
209
|
+
const error = new TypeError(ValidationErrorCode.InvalidProperties);
|
|
198
210
|
Object.assign(error, {
|
|
199
211
|
errors
|
|
200
212
|
});
|
|
201
213
|
throw error;
|
|
202
214
|
}
|
|
203
215
|
return left(makeValidationError({
|
|
204
|
-
code:
|
|
216
|
+
code: ValidationErrorCode.InvalidProperties,
|
|
205
217
|
errors
|
|
206
218
|
}));
|
|
207
219
|
}
|
|
208
220
|
return right(resultCopy);
|
|
209
221
|
};
|
|
210
|
-
export const validateSilently = (what, schema, options = {}) => {
|
|
211
|
-
const resultEither = validate(what, schema, options);
|
|
212
|
-
return isLeft(resultEither) ? null : unwrapEither(resultEither);
|
|
213
|
-
};
|
|
214
222
|
export const validator = (schema, options = {}) => {
|
|
215
223
|
return [
|
|
216
224
|
{},
|
|
@@ -219,11 +227,3 @@ export const validator = (schema, options = {}) => {
|
|
|
219
227
|
}
|
|
220
228
|
];
|
|
221
229
|
};
|
|
222
|
-
export const silentValidator = (schema, options = {}) => {
|
|
223
|
-
return [
|
|
224
|
-
{},
|
|
225
|
-
(what) => {
|
|
226
|
-
return validateSilently(what, schema, options);
|
|
227
|
-
}
|
|
228
|
-
];
|
|
229
|
-
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/validation",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.58",
|
|
4
4
|
"description": "## Installation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"@aeriajs/types": "link:../types"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"@aeriajs/common": "^0.0.
|
|
30
|
-
"@aeriajs/types": "^0.0.
|
|
29
|
+
"@aeriajs/common": "^0.0.55",
|
|
30
|
+
"@aeriajs/types": "^0.0.52"
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
33
|
"test": "env TS_NODE_COMPILER_OPTIONS=\"$(cat ../compilerOptions.json)\" mocha -r ts-node/register tests/*.spec.ts",
|