@aeriajs/validation 0.0.123 → 0.0.124

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.
@@ -2,6 +2,7 @@ import type { JsonSchema, Property, InferSchema, Description, PropertyValidation
2
2
  import { Result } from '@aeriajs/types';
3
3
  import { ValidationErrorCode } from '@aeriajs/types';
4
4
  export type ValidateOptions = {
5
+ tolerateExtraneous?: boolean;
5
6
  filterOutExtraneous?: boolean;
6
7
  throwOnError?: boolean;
7
8
  coerce?: boolean;
package/dist/validate.js CHANGED
@@ -41,17 +41,16 @@ const makeValidationError = (error) => {
41
41
  };
42
42
  exports.makeValidationError = makeValidationError;
43
43
  const validateProperty = (propName, what, property, options = {}) => {
44
- const { filterOutExtraneous, coerce } = options;
45
44
  if (!property) {
46
45
  if (options.parentProperty && 'additionalProperties' in options.parentProperty && options.parentProperty.additionalProperties) {
47
- const extraneous = options.parentProperty.additionalProperties;
48
- if (typeof extraneous === 'boolean' || Object.keys(extraneous).includes(propName)) {
49
- if (filterOutExtraneous) {
50
- return types_1.Result.result(undefined);
51
- }
46
+ if (options.filterOutExtraneous) {
47
+ return types_1.Result.result(undefined);
52
48
  }
53
49
  return types_1.Result.result(what);
54
50
  }
51
+ if (options.tolerateExtraneous) {
52
+ return types_1.Result.result(undefined);
53
+ }
55
54
  return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Extraneous));
56
55
  }
57
56
  if (what === null || what === undefined) {
@@ -88,7 +87,7 @@ const validateProperty = (propName, what, property, options = {}) => {
88
87
  return types_1.Result.result(what);
89
88
  }
90
89
  }
91
- if (coerce) {
90
+ if (options.coerce) {
92
91
  if (expectedType === 'number' && typeof what === 'string') {
93
92
  const coerced = parseFloat(what);
94
93
  if (!isNaN(coerced)) {
@@ -212,6 +211,9 @@ const validate = (what, schema, options = {}) => {
212
211
  }
213
212
  const wholenessError = (0, exports.validateWholeness)(what, schema);
214
213
  if (wholenessError) {
214
+ if (options.throwOnError) {
215
+ throw new Error(types_2.ValidationErrorCode.MissingProperties);
216
+ }
215
217
  return types_1.Result.error(wholenessError);
216
218
  }
217
219
  const errors = {};
package/dist/validate.mjs CHANGED
@@ -34,17 +34,16 @@ export const makeValidationError = (error) => {
34
34
  return error;
35
35
  };
36
36
  export const validateProperty = (propName, what, property, options = {}) => {
37
- const { filterOutExtraneous, coerce } = options;
38
37
  if (!property) {
39
38
  if (options.parentProperty && "additionalProperties" in options.parentProperty && options.parentProperty.additionalProperties) {
40
- const extraneous = options.parentProperty.additionalProperties;
41
- if (typeof extraneous === "boolean" || Object.keys(extraneous).includes(propName)) {
42
- if (filterOutExtraneous) {
43
- return Result.result(void 0);
44
- }
39
+ if (options.filterOutExtraneous) {
40
+ return Result.result(void 0);
45
41
  }
46
42
  return Result.result(what);
47
43
  }
44
+ if (options.tolerateExtraneous) {
45
+ return Result.result(void 0);
46
+ }
48
47
  return Result.error(makePropertyError(PropertyValidationErrorCode.Extraneous));
49
48
  }
50
49
  if (what === null || what === void 0) {
@@ -79,7 +78,7 @@ export const validateProperty = (propName, what, property, options = {}) => {
79
78
  return Result.result(what);
80
79
  }
81
80
  }
82
- if (coerce) {
81
+ if (options.coerce) {
83
82
  if (expectedType === "number" && typeof what === "string") {
84
83
  const coerced = parseFloat(what);
85
84
  if (!isNaN(coerced)) {
@@ -191,6 +190,9 @@ export const validate = (what, schema, options = {}) => {
191
190
  }
192
191
  const wholenessError = validateWholeness(what, schema);
193
192
  if (wholenessError) {
193
+ if (options.throwOnError) {
194
+ throw new Error(ValidationErrorCode.MissingProperties);
195
+ }
194
196
  return Result.error(wholenessError);
195
197
  }
196
198
  const errors = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/validation",
3
- "version": "0.0.123",
3
+ "version": "0.0.124",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",