@aeriajs/validation 0.0.126 → 0.0.127

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.js CHANGED
@@ -4,11 +4,6 @@ exports.validator = exports.validate = exports.validateWholeness = exports.valid
4
4
  const types_1 = require("@aeriajs/types");
5
5
  const common_1 = require("@aeriajs/common");
6
6
  const types_2 = require("@aeriajs/types");
7
- const getValueType = (value) => {
8
- return Array.isArray(value)
9
- ? 'array'
10
- : typeof value;
11
- };
12
7
  const getPropertyType = (property) => {
13
8
  if ('type' in property) {
14
9
  if ('format' in property && property.format) {
@@ -45,7 +40,7 @@ exports.makeValidationError = makeValidationError;
45
40
  const validateProperty = (what, property, options = {}) => {
46
41
  if (!property) {
47
42
  if (options.parentProperty && 'additionalProperties' in options.parentProperty && options.parentProperty.additionalProperties) {
48
- if (typeof options.parentProperty.additionalProperties === 'boolean') {
43
+ if (options.parentProperty.additionalProperties === true) {
49
44
  return types_1.Result.result(what);
50
45
  }
51
46
  return (0, exports.validateProperty)(what, options.parentProperty.additionalProperties);
@@ -62,7 +57,9 @@ const validateProperty = (what, property, options = {}) => {
62
57
  return types_1.Result.result(undefined);
63
58
  }
64
59
  const expectedType = getPropertyType(property);
65
- const actualType = getValueType(what);
60
+ const actualType = Array.isArray(what)
61
+ ? 'array'
62
+ : typeof what;
66
63
  if (actualType !== expectedType
67
64
  && !(actualType === 'number' && expectedType === 'integer')) {
68
65
  if (expectedType === 'datetime' && what instanceof Date) {
@@ -185,8 +182,7 @@ const validateWholeness = (what, schema) => {
185
182
  if (missingProps.length > 0) {
186
183
  return (0, exports.makeValidationError)({
187
184
  code: types_2.ValidationErrorCode.MissingProperties,
188
- errors: Object.fromEntries(missingProps
189
- .map((error) => [
185
+ errors: Object.fromEntries(missingProps.map((error) => [
190
186
  error,
191
187
  {
192
188
  type: 'missing',
@@ -197,7 +193,7 @@ const validateWholeness = (what, schema) => {
197
193
  };
198
194
  exports.validateWholeness = validateWholeness;
199
195
  const validate = (what, schema, options = {}) => {
200
- if (!what) {
196
+ if (what === undefined) {
201
197
  return types_1.Result.error((0, exports.makeValidationError)({
202
198
  code: types_2.ValidationErrorCode.EmptyTarget,
203
199
  errors: {},
@@ -205,14 +201,15 @@ const validate = (what, schema, options = {}) => {
205
201
  }
206
202
  if (!('properties' in schema)) {
207
203
  const { error } = (0, exports.validateProperty)(what, schema);
208
- return error
209
- ? types_1.Result.error(error)
210
- : types_1.Result.result(what);
204
+ if (error) {
205
+ return types_1.Result.error(error);
206
+ }
207
+ return types_1.Result.result(what);
211
208
  }
212
209
  const wholenessError = (0, exports.validateWholeness)(what, schema);
213
210
  if (wholenessError) {
214
211
  if (options.throwOnError) {
215
- throw new Error(types_2.ValidationErrorCode.MissingProperties);
212
+ throw new TypeError(types_2.ValidationErrorCode.MissingProperties);
216
213
  }
217
214
  return types_1.Result.error(wholenessError);
218
215
  }
@@ -224,20 +221,17 @@ const validate = (what, schema, options = {}) => {
224
221
  parentProperty: schema,
225
222
  });
226
223
  if (error) {
224
+ if (options.throwOnError) {
225
+ throw new TypeError(types_2.ValidationErrorCode.InvalidProperties);
226
+ }
227
227
  errors[propName] = error;
228
+ continue;
228
229
  }
229
230
  if (parsed !== undefined) {
230
231
  resultCopy[propName] = parsed;
231
232
  }
232
233
  }
233
234
  if (Object.keys(errors).length > 0) {
234
- if (options.throwOnError) {
235
- const error = new TypeError(types_2.ValidationErrorCode.InvalidProperties);
236
- Object.assign(error, {
237
- errors,
238
- });
239
- throw error;
240
- }
241
235
  return types_1.Result.error((0, exports.makeValidationError)({
242
236
  code: types_2.ValidationErrorCode.InvalidProperties,
243
237
  errors,
package/dist/validate.mjs CHANGED
@@ -2,9 +2,6 @@
2
2
  import { Result } from "@aeriajs/types";
3
3
  import { getMissingProperties } from "@aeriajs/common";
4
4
  import { ValidationErrorCode, PropertyValidationErrorCode } from "@aeriajs/types";
5
- const getValueType = (value) => {
6
- return Array.isArray(value) ? "array" : typeof value;
7
- };
8
5
  const getPropertyType = (property) => {
9
6
  if ("type" in property) {
10
7
  if ("format" in property && property.format) {
@@ -38,13 +35,10 @@ export const makeValidationError = (error) => {
38
35
  export const validateProperty = (what, property, options = {}) => {
39
36
  if (!property) {
40
37
  if (options.parentProperty && "additionalProperties" in options.parentProperty && options.parentProperty.additionalProperties) {
41
- if (typeof options.parentProperty.additionalProperties === "boolean") {
38
+ if (options.parentProperty.additionalProperties === true) {
42
39
  return Result.result(what);
43
40
  }
44
- return validateProperty(
45
- what,
46
- options.parentProperty.additionalProperties
47
- );
41
+ return validateProperty(what, options.parentProperty.additionalProperties);
48
42
  }
49
43
  if (options.tolerateExtraneous) {
50
44
  return Result.result(void 0);
@@ -58,7 +52,7 @@ export const validateProperty = (what, property, options = {}) => {
58
52
  return Result.result(void 0);
59
53
  }
60
54
  const expectedType = getPropertyType(property);
61
- const actualType = getValueType(what);
55
+ const actualType = Array.isArray(what) ? "array" : typeof what;
62
56
  if (actualType !== expectedType && !(actualType === "number" && expectedType === "integer")) {
63
57
  if (expectedType === "datetime" && what instanceof Date) {
64
58
  return Result.result(what);
@@ -182,7 +176,7 @@ export const validateWholeness = (what, schema) => {
182
176
  }
183
177
  };
184
178
  export const validate = (what, schema, options = {}) => {
185
- if (!what) {
179
+ if (what === void 0) {
186
180
  return Result.error(makeValidationError({
187
181
  code: ValidationErrorCode.EmptyTarget,
188
182
  errors: {}
@@ -190,12 +184,15 @@ export const validate = (what, schema, options = {}) => {
190
184
  }
191
185
  if (!("properties" in schema)) {
192
186
  const { error } = validateProperty(what, schema);
193
- return error ? Result.error(error) : Result.result(what);
187
+ if (error) {
188
+ return Result.error(error);
189
+ }
190
+ return Result.result(what);
194
191
  }
195
192
  const wholenessError = validateWholeness(what, schema);
196
193
  if (wholenessError) {
197
194
  if (options.throwOnError) {
198
- throw new Error(ValidationErrorCode.MissingProperties);
195
+ throw new TypeError(ValidationErrorCode.MissingProperties);
199
196
  }
200
197
  return Result.error(wholenessError);
201
198
  }
@@ -207,20 +204,17 @@ export const validate = (what, schema, options = {}) => {
207
204
  parentProperty: schema
208
205
  });
209
206
  if (error) {
207
+ if (options.throwOnError) {
208
+ throw new TypeError(ValidationErrorCode.InvalidProperties);
209
+ }
210
210
  errors[propName] = error;
211
+ continue;
211
212
  }
212
213
  if (parsed !== void 0) {
213
214
  resultCopy[propName] = parsed;
214
215
  }
215
216
  }
216
217
  if (Object.keys(errors).length > 0) {
217
- if (options.throwOnError) {
218
- const error = new TypeError(ValidationErrorCode.InvalidProperties);
219
- Object.assign(error, {
220
- errors
221
- });
222
- throw error;
223
- }
224
218
  return Result.error(makeValidationError({
225
219
  code: ValidationErrorCode.InvalidProperties,
226
220
  errors
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/validation",
3
- "version": "0.0.126",
3
+ "version": "0.0.127",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",