@aeriajs/validation 0.0.124 → 0.0.126

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.
@@ -3,13 +3,12 @@ import { Result } from '@aeriajs/types';
3
3
  import { ValidationErrorCode } from '@aeriajs/types';
4
4
  export type ValidateOptions = {
5
5
  tolerateExtraneous?: boolean;
6
- filterOutExtraneous?: boolean;
7
6
  throwOnError?: boolean;
8
7
  coerce?: boolean;
9
8
  parentProperty?: Omit<Description, '$id'> | Property;
10
9
  };
11
10
  export declare const makeValidationError: <TValidationError extends ValidationError>(error: TValidationError) => TValidationError;
12
- export declare const validateProperty: <TWhat>(propName: string, what: TWhat, property: Property | undefined, options?: ValidateOptions) => Result.Either<PropertyValidationError | ValidationError, unknown>;
11
+ export declare const validateProperty: <TWhat>(what: TWhat, property: Property | undefined, options?: ValidateOptions) => Result.Either<PropertyValidationError | ValidationError, unknown>;
13
12
  export declare const validateWholeness: (what: Record<string, unknown>, schema: Omit<JsonSchema, "$id">) => {
14
13
  code: ValidationErrorCode.MissingProperties;
15
14
  errors: {
package/dist/validate.js CHANGED
@@ -12,11 +12,10 @@ const getValueType = (value) => {
12
12
  const getPropertyType = (property) => {
13
13
  if ('type' in property) {
14
14
  if ('format' in property && property.format) {
15
- if ([
16
- 'date',
17
- 'date-time',
18
- ].includes(property.format)) {
19
- return 'datetime';
15
+ switch (property.format) {
16
+ case 'date':
17
+ case 'date-time':
18
+ return 'datetime';
20
19
  }
21
20
  }
22
21
  return property.type;
@@ -24,9 +23,12 @@ const getPropertyType = (property) => {
24
23
  if ('enum' in property) {
25
24
  return typeof property.enum[0];
26
25
  }
27
- if ('$ref' in property
28
- || 'properties' in property
29
- || 'additionalProperties' in property) {
26
+ if ('const' in property) {
27
+ return typeof property.const;
28
+ }
29
+ if ('properties' in property
30
+ || 'additionalProperties' in property
31
+ || '$ref' in property) {
30
32
  return 'object';
31
33
  }
32
34
  };
@@ -40,13 +42,13 @@ const makeValidationError = (error) => {
40
42
  return error;
41
43
  };
42
44
  exports.makeValidationError = makeValidationError;
43
- const validateProperty = (propName, what, property, options = {}) => {
45
+ const validateProperty = (what, property, options = {}) => {
44
46
  if (!property) {
45
47
  if (options.parentProperty && 'additionalProperties' in options.parentProperty && options.parentProperty.additionalProperties) {
46
- if (options.filterOutExtraneous) {
47
- return types_1.Result.result(undefined);
48
+ if (typeof options.parentProperty.additionalProperties === 'boolean') {
49
+ return types_1.Result.result(what);
48
50
  }
49
- return types_1.Result.result(what);
51
+ return (0, exports.validateProperty)(what, options.parentProperty.additionalProperties);
50
52
  }
51
53
  if (options.tolerateExtraneous) {
52
54
  return types_1.Result.result(undefined);
@@ -56,34 +58,18 @@ const validateProperty = (propName, what, property, options = {}) => {
56
58
  if (what === null || what === undefined) {
57
59
  return types_1.Result.result(what);
58
60
  }
59
- if ('properties' in property) {
60
- return (0, exports.validate)(what, property, options);
61
- }
62
- if ('const' in property) {
63
- if (what !== property.const) {
64
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
65
- expected: property.const,
66
- got: what,
67
- }));
68
- }
69
- return types_1.Result.result(what);
70
- }
71
- if ('enum' in property && property.enum.length === 0) {
72
- return types_1.Result.result(what);
73
- }
74
61
  if ('getter' in property) {
75
62
  return types_1.Result.result(undefined);
76
63
  }
77
64
  const expectedType = getPropertyType(property);
78
65
  const actualType = getValueType(what);
79
66
  if (actualType !== expectedType
80
- && !('items' in property && actualType === 'array')
81
67
  && !(actualType === 'number' && expectedType === 'integer')) {
82
68
  if (expectedType === 'datetime' && what instanceof Date) {
83
69
  return types_1.Result.result(what);
84
70
  }
85
71
  if ('$ref' in property && typeof what === 'string') {
86
- if (/^[0-9a-fA-F]{24}$/.test(what)) {
72
+ if (/^[0-9a-f]{24}$/.test(what)) {
87
73
  return types_1.Result.result(what);
88
74
  }
89
75
  }
@@ -109,71 +95,85 @@ const validateProperty = (propName, what, property, options = {}) => {
109
95
  got: actualType,
110
96
  }));
111
97
  }
112
- if ('items' in property) {
113
- if (!Array.isArray(what)) {
114
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
115
- expected: expectedType,
116
- got: actualType,
117
- }));
118
- }
119
- if (property.minItems) {
120
- if (what.length < property.minItems) {
121
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.MoreItemsExpected));
122
- }
123
- }
124
- if (property.maxItems) {
125
- if (what.length > property.maxItems) {
126
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.LessItemsExpected));
127
- }
128
- }
129
- let i = 0;
130
- for (const elem of what) {
131
- const { error } = (0, exports.validateProperty)(propName, elem, property.items, options);
132
- if (error) {
133
- if ('errors' in error) {
134
- continue;
98
+ if ('type' in property) {
99
+ switch (property.type) {
100
+ case 'integer': {
101
+ if (!Number.isInteger(what)) {
102
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
103
+ expected: 'integer',
104
+ got: 'invalid_number',
105
+ }));
135
106
  }
136
- error.index = i;
137
- return types_1.Result.error(error);
138
107
  }
139
- i++;
140
- }
141
- }
142
- else if ('type' in property) {
143
- if (property.type === 'integer') {
144
- if (!Number.isInteger(what)) {
145
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
146
- expected: 'integer',
147
- got: 'invalid_number',
148
- }));
108
+ case 'number': {
109
+ if (typeof what !== 'number') {
110
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
111
+ expected: expectedType,
112
+ got: actualType,
113
+ }));
114
+ }
115
+ if ((typeof property.maximum === 'number' && property.maximum < what)
116
+ || (typeof property.minimum === 'number' && property.minimum > what)
117
+ || (typeof property.exclusiveMaximum === 'number' && property.exclusiveMaximum <= what)
118
+ || (typeof property.exclusiveMinimum === 'number' && property.exclusiveMinimum >= what)) {
119
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
120
+ expected: 'number',
121
+ got: 'invalid_number',
122
+ }));
123
+ }
124
+ break;
149
125
  }
150
- }
151
- if (property.type === 'integer' || property.type === 'number') {
152
- if (typeof what !== 'number') {
153
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
154
- expected: expectedType,
155
- got: actualType,
156
- }));
126
+ case 'object': {
127
+ return (0, exports.validate)(what, property, options);
157
128
  }
158
- if ((property.maximum && property.maximum < what)
159
- || (property.minimum && property.minimum > what)
160
- || (property.exclusiveMaximum && property.exclusiveMaximum <= what)
161
- || (property.exclusiveMinimum && property.exclusiveMinimum >= what)) {
162
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
163
- expected: 'number',
164
- got: 'invalid_number',
165
- }));
129
+ case 'array': {
130
+ if (!Array.isArray(what)) {
131
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
132
+ expected: expectedType,
133
+ got: actualType,
134
+ }));
135
+ }
136
+ if (property.minItems) {
137
+ if (what.length < property.minItems) {
138
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.MoreItemsExpected));
139
+ }
140
+ }
141
+ if (property.maxItems) {
142
+ if (what.length > property.maxItems) {
143
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.LessItemsExpected));
144
+ }
145
+ }
146
+ let i = 0;
147
+ for (const elem of what) {
148
+ const { error } = (0, exports.validateProperty)(elem, property.items, options);
149
+ if (error) {
150
+ if ('errors' in error) {
151
+ continue;
152
+ }
153
+ error.index = i;
154
+ return types_1.Result.error(error);
155
+ }
156
+ i++;
157
+ }
166
158
  }
167
159
  }
168
160
  }
169
161
  else if ('enum' in property) {
170
- if (!property.enum.includes(what)) {
162
+ if (!property.enum.includes(what) && property.enum.length === 0) {
171
163
  return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.ExtraneousElement, {
172
164
  expected: property.enum,
173
165
  got: what,
174
166
  }));
175
167
  }
176
168
  }
169
+ else if ('const' in property) {
170
+ if (what !== property.const) {
171
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
172
+ expected: property.const,
173
+ got: what,
174
+ }));
175
+ }
176
+ }
177
177
  return types_1.Result.result(what);
178
178
  };
179
179
  exports.validateProperty = validateProperty;
@@ -204,7 +204,7 @@ const validate = (what, schema, options = {}) => {
204
204
  }));
205
205
  }
206
206
  if (!('properties' in schema)) {
207
- const { error } = (0, exports.validateProperty)('', what, schema);
207
+ const { error } = (0, exports.validateProperty)(what, schema);
208
208
  return error
209
209
  ? types_1.Result.error(error)
210
210
  : types_1.Result.result(what);
@@ -219,7 +219,7 @@ const validate = (what, schema, options = {}) => {
219
219
  const errors = {};
220
220
  const resultCopy = {};
221
221
  for (const propName in what) {
222
- const { error, result: parsed } = (0, exports.validateProperty)(propName, what[propName], schema.properties[propName], {
222
+ const { error, result: parsed } = (0, exports.validateProperty)(what[propName], schema.properties[propName], {
223
223
  ...options,
224
224
  parentProperty: schema,
225
225
  });
package/dist/validate.mjs CHANGED
@@ -8,11 +8,10 @@ const getValueType = (value) => {
8
8
  const getPropertyType = (property) => {
9
9
  if ("type" in property) {
10
10
  if ("format" in property && property.format) {
11
- if ([
12
- "date",
13
- "date-time"
14
- ].includes(property.format)) {
15
- return "datetime";
11
+ switch (property.format) {
12
+ case "date":
13
+ case "date-time":
14
+ return "datetime";
16
15
  }
17
16
  }
18
17
  return property.type;
@@ -20,7 +19,10 @@ const getPropertyType = (property) => {
20
19
  if ("enum" in property) {
21
20
  return typeof property.enum[0];
22
21
  }
23
- if ("$ref" in property || "properties" in property || "additionalProperties" in property) {
22
+ if ("const" in property) {
23
+ return typeof property.const;
24
+ }
25
+ if ("properties" in property || "additionalProperties" in property || "$ref" in property) {
24
26
  return "object";
25
27
  }
26
28
  };
@@ -33,13 +35,16 @@ const makePropertyError = (type, details) => {
33
35
  export const makeValidationError = (error) => {
34
36
  return error;
35
37
  };
36
- export const validateProperty = (propName, what, property, options = {}) => {
38
+ export const validateProperty = (what, property, options = {}) => {
37
39
  if (!property) {
38
40
  if (options.parentProperty && "additionalProperties" in options.parentProperty && options.parentProperty.additionalProperties) {
39
- if (options.filterOutExtraneous) {
40
- return Result.result(void 0);
41
+ if (typeof options.parentProperty.additionalProperties === "boolean") {
42
+ return Result.result(what);
41
43
  }
42
- return Result.result(what);
44
+ return validateProperty(
45
+ what,
46
+ options.parentProperty.additionalProperties
47
+ );
43
48
  }
44
49
  if (options.tolerateExtraneous) {
45
50
  return Result.result(void 0);
@@ -49,32 +54,17 @@ export const validateProperty = (propName, what, property, options = {}) => {
49
54
  if (what === null || what === void 0) {
50
55
  return Result.result(what);
51
56
  }
52
- if ("properties" in property) {
53
- return validate(what, property, options);
54
- }
55
- if ("const" in property) {
56
- if (what !== property.const) {
57
- return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
58
- expected: property.const,
59
- got: what
60
- }));
61
- }
62
- return Result.result(what);
63
- }
64
- if ("enum" in property && property.enum.length === 0) {
65
- return Result.result(what);
66
- }
67
57
  if ("getter" in property) {
68
58
  return Result.result(void 0);
69
59
  }
70
60
  const expectedType = getPropertyType(property);
71
61
  const actualType = getValueType(what);
72
- if (actualType !== expectedType && !("items" in property && actualType === "array") && !(actualType === "number" && expectedType === "integer")) {
62
+ if (actualType !== expectedType && !(actualType === "number" && expectedType === "integer")) {
73
63
  if (expectedType === "datetime" && what instanceof Date) {
74
64
  return Result.result(what);
75
65
  }
76
66
  if ("$ref" in property && typeof what === "string") {
77
- if (/^[0-9a-fA-F]{24}$/.test(what)) {
67
+ if (/^[0-9a-f]{24}$/.test(what)) {
78
68
  return Result.result(what);
79
69
  }
80
70
  }
@@ -100,65 +90,79 @@ export const validateProperty = (propName, what, property, options = {}) => {
100
90
  got: actualType
101
91
  }));
102
92
  }
103
- if ("items" in property) {
104
- if (!Array.isArray(what)) {
105
- return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
106
- expected: expectedType,
107
- got: actualType
108
- }));
109
- }
110
- if (property.minItems) {
111
- if (what.length < property.minItems) {
112
- return Result.error(makePropertyError(PropertyValidationErrorCode.MoreItemsExpected));
113
- }
114
- }
115
- if (property.maxItems) {
116
- if (what.length > property.maxItems) {
117
- return Result.error(makePropertyError(PropertyValidationErrorCode.LessItemsExpected));
118
- }
119
- }
120
- let i = 0;
121
- for (const elem of what) {
122
- const { error } = validateProperty(propName, elem, property.items, options);
123
- if (error) {
124
- if ("errors" in error) {
125
- continue;
93
+ if ("type" in property) {
94
+ switch (property.type) {
95
+ case "integer": {
96
+ if (!Number.isInteger(what)) {
97
+ return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
98
+ expected: "integer",
99
+ got: "invalid_number"
100
+ }));
126
101
  }
127
- error.index = i;
128
- return Result.error(error);
129
102
  }
130
- i++;
131
- }
132
- } else if ("type" in property) {
133
- if (property.type === "integer") {
134
- if (!Number.isInteger(what)) {
135
- return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
136
- expected: "integer",
137
- got: "invalid_number"
138
- }));
103
+ case "number": {
104
+ if (typeof what !== "number") {
105
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
106
+ expected: expectedType,
107
+ got: actualType
108
+ }));
109
+ }
110
+ if (typeof property.maximum === "number" && property.maximum < what || typeof property.minimum === "number" && property.minimum > what || typeof property.exclusiveMaximum === "number" && property.exclusiveMaximum <= what || typeof property.exclusiveMinimum === "number" && property.exclusiveMinimum >= what) {
111
+ return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
112
+ expected: "number",
113
+ got: "invalid_number"
114
+ }));
115
+ }
116
+ break;
139
117
  }
140
- }
141
- if (property.type === "integer" || property.type === "number") {
142
- if (typeof what !== "number") {
143
- return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
144
- expected: expectedType,
145
- got: actualType
146
- }));
118
+ case "object": {
119
+ return validate(what, property, options);
147
120
  }
148
- if (property.maximum && property.maximum < what || property.minimum && property.minimum > what || property.exclusiveMaximum && property.exclusiveMaximum <= what || property.exclusiveMinimum && property.exclusiveMinimum >= what) {
149
- return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
150
- expected: "number",
151
- got: "invalid_number"
152
- }));
121
+ case "array": {
122
+ if (!Array.isArray(what)) {
123
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
124
+ expected: expectedType,
125
+ got: actualType
126
+ }));
127
+ }
128
+ if (property.minItems) {
129
+ if (what.length < property.minItems) {
130
+ return Result.error(makePropertyError(PropertyValidationErrorCode.MoreItemsExpected));
131
+ }
132
+ }
133
+ if (property.maxItems) {
134
+ if (what.length > property.maxItems) {
135
+ return Result.error(makePropertyError(PropertyValidationErrorCode.LessItemsExpected));
136
+ }
137
+ }
138
+ let i = 0;
139
+ for (const elem of what) {
140
+ const { error } = validateProperty(elem, property.items, options);
141
+ if (error) {
142
+ if ("errors" in error) {
143
+ continue;
144
+ }
145
+ error.index = i;
146
+ return Result.error(error);
147
+ }
148
+ i++;
149
+ }
153
150
  }
154
151
  }
155
152
  } else if ("enum" in property) {
156
- if (!property.enum.includes(what)) {
153
+ if (!property.enum.includes(what) && property.enum.length === 0) {
157
154
  return Result.error(makePropertyError(PropertyValidationErrorCode.ExtraneousElement, {
158
155
  expected: property.enum,
159
156
  got: what
160
157
  }));
161
158
  }
159
+ } else if ("const" in property) {
160
+ if (what !== property.const) {
161
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
162
+ expected: property.const,
163
+ got: what
164
+ }));
165
+ }
162
166
  }
163
167
  return Result.result(what);
164
168
  };
@@ -185,7 +189,7 @@ export const validate = (what, schema, options = {}) => {
185
189
  }));
186
190
  }
187
191
  if (!("properties" in schema)) {
188
- const { error } = validateProperty("", what, schema);
192
+ const { error } = validateProperty(what, schema);
189
193
  return error ? Result.error(error) : Result.result(what);
190
194
  }
191
195
  const wholenessError = validateWholeness(what, schema);
@@ -198,15 +202,10 @@ export const validate = (what, schema, options = {}) => {
198
202
  const errors = {};
199
203
  const resultCopy = {};
200
204
  for (const propName in what) {
201
- const { error, result: parsed } = validateProperty(
202
- propName,
203
- what[propName],
204
- schema.properties[propName],
205
- {
206
- ...options,
207
- parentProperty: schema
208
- }
209
- );
205
+ const { error, result: parsed } = validateProperty(what[propName], schema.properties[propName], {
206
+ ...options,
207
+ parentProperty: schema
208
+ });
210
209
  if (error) {
211
210
  errors[propName] = error;
212
211
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/validation",
3
- "version": "0.0.124",
3
+ "version": "0.0.126",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -26,7 +26,7 @@
26
26
  "@aeriajs/types": "link:../types"
27
27
  },
28
28
  "peerDependencies": {
29
- "@aeriajs/common": "^0.0.119",
29
+ "@aeriajs/common": "^0.0.120",
30
30
  "@aeriajs/types": "^0.0.102",
31
31
  "mongodb": "^6.5.0"
32
32
  },