@aeriajs/validation 0.0.71 → 0.0.72

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.
@@ -1,5 +1,5 @@
1
1
  import type { JsonSchema, Property, InferSchema, Description, PropertyValidationError, ValidationError } from '@aeriajs/types';
2
- import { Result } from '@aeriajs/common';
2
+ import { Result } from '@aeriajs/types';
3
3
  import { ValidationErrorCode } from '@aeriajs/types';
4
4
  export type ValidateOptions = {
5
5
  extraneous?: string[] | boolean;
package/dist/validate.js CHANGED
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validator = exports.validate = exports.validateWholeness = exports.validateProperty = exports.makeValidationError = void 0;
4
- const common_1 = require("@aeriajs/common");
5
4
  const types_1 = require("@aeriajs/types");
5
+ const common_1 = require("@aeriajs/common");
6
+ const types_2 = require("@aeriajs/types");
6
7
  const getValueType = (value) => {
7
8
  return Array.isArray(value)
8
9
  ? 'array'
@@ -42,79 +43,79 @@ exports.makeValidationError = makeValidationError;
42
43
  const validateProperty = (propName, what, property, options = {}) => {
43
44
  const { extraneous, filterOutExtraneous, coerce } = options;
44
45
  if (what === undefined) {
45
- return common_1.Result.result(what);
46
+ return types_1.Result.result(what);
46
47
  }
47
48
  if (!property) {
48
49
  if (extraneous || (Array.isArray(extraneous) && extraneous.includes(propName))) {
49
50
  if (filterOutExtraneous) {
50
- return common_1.Result.result(undefined);
51
+ return types_1.Result.result(undefined);
51
52
  }
52
- return common_1.Result.result(what);
53
+ return types_1.Result.result(what);
53
54
  }
54
- return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Extraneous, {
55
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Extraneous, {
55
56
  expected: 'undefined',
56
57
  got: getValueType(what),
57
58
  }));
58
59
  }
59
60
  if ('getter' in property) {
60
- return common_1.Result.result(undefined);
61
+ return types_1.Result.result(undefined);
61
62
  }
62
63
  if ('properties' in property) {
63
64
  return (0, exports.validate)(what, property, options);
64
65
  }
65
66
  if ('const' in property) {
66
67
  if (what !== property.const) {
67
- return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
68
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
68
69
  expected: property.const,
69
70
  got: what,
70
71
  }));
71
72
  }
72
- return common_1.Result.result(what);
73
+ return types_1.Result.result(what);
73
74
  }
74
75
  const expectedType = getPropertyType(property);
75
76
  const actualType = getValueType(what);
76
77
  if ('enum' in property && property.enum.length === 0) {
77
- return common_1.Result.result(what);
78
+ return types_1.Result.result(what);
78
79
  }
79
80
  if (actualType !== expectedType
80
81
  && !('items' in property && actualType === 'array')
81
82
  && !(actualType === 'number' && expectedType === 'integer')) {
82
83
  if (expectedType === 'datetime' && what instanceof Date) {
83
- return common_1.Result.result(what);
84
+ return types_1.Result.result(what);
84
85
  }
85
86
  if (expectedType === 'boolean' && !what) {
86
- return common_1.Result.result(what);
87
+ return types_1.Result.result(what);
87
88
  }
88
89
  if ('$ref' in property && typeof what === 'string') {
89
90
  if (/^[0-9a-fA-F]{24}$/.test(what)) {
90
- return common_1.Result.result(what);
91
+ return types_1.Result.result(what);
91
92
  }
92
93
  }
93
94
  if (coerce) {
94
95
  if (expectedType === 'number' && typeof what === 'string') {
95
96
  const coerced = parseFloat(what);
96
97
  if (!isNaN(coerced)) {
97
- return common_1.Result.result(coerced);
98
+ return types_1.Result.result(coerced);
98
99
  }
99
100
  }
100
101
  if (expectedType === 'integer' && typeof what === 'string') {
101
102
  const coerced = parseInt(what);
102
103
  if (!isNaN(coerced)) {
103
- return common_1.Result.result(coerced);
104
+ return types_1.Result.result(coerced);
104
105
  }
105
106
  }
106
107
  if (expectedType === 'string' && typeof what === 'number') {
107
- return common_1.Result.result(String(what));
108
+ return types_1.Result.result(String(what));
108
109
  }
109
110
  }
110
- return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
111
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
111
112
  expected: expectedType,
112
113
  got: actualType,
113
114
  }));
114
115
  }
115
116
  if ('items' in property) {
116
117
  if (!Array.isArray(what)) {
117
- return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
118
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
118
119
  expected: expectedType,
119
120
  got: actualType,
120
121
  }));
@@ -127,7 +128,7 @@ const validateProperty = (propName, what, property, options = {}) => {
127
128
  continue;
128
129
  }
129
130
  error.index = i;
130
- return common_1.Result.error(error);
131
+ return types_1.Result.error(error);
131
132
  }
132
133
  i++;
133
134
  }
@@ -135,7 +136,7 @@ const validateProperty = (propName, what, property, options = {}) => {
135
136
  else if ('type' in property) {
136
137
  if (property.type === 'integer') {
137
138
  if (!Number.isInteger(what)) {
138
- return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
139
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
139
140
  expected: 'integer',
140
141
  got: 'invalid_number',
141
142
  }));
@@ -143,7 +144,7 @@ const validateProperty = (propName, what, property, options = {}) => {
143
144
  }
144
145
  if (property.type === 'integer' || property.type === 'number') {
145
146
  if (typeof what !== 'number') {
146
- return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
147
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
147
148
  expected: expectedType,
148
149
  got: actualType,
149
150
  }));
@@ -152,7 +153,7 @@ const validateProperty = (propName, what, property, options = {}) => {
152
153
  || (property.minimum && property.minimum > what)
153
154
  || (property.exclusiveMaximum && property.exclusiveMaximum <= what)
154
155
  || (property.exclusiveMinimum && property.exclusiveMinimum >= what)) {
155
- return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
156
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
156
157
  expected: 'number',
157
158
  got: 'invalid_number',
158
159
  }));
@@ -161,13 +162,13 @@ const validateProperty = (propName, what, property, options = {}) => {
161
162
  }
162
163
  else if ('enum' in property) {
163
164
  if (!property.enum.includes(what)) {
164
- return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.ExtraneousElement, {
165
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.ExtraneousElement, {
165
166
  expected: property.enum,
166
167
  got: what,
167
168
  }));
168
169
  }
169
170
  }
170
- return common_1.Result.result(what);
171
+ return types_1.Result.result(what);
171
172
  };
172
173
  exports.validateProperty = validateProperty;
173
174
  const validateWholeness = (what, schema) => {
@@ -177,7 +178,7 @@ const validateWholeness = (what, schema) => {
177
178
  const missingProps = (0, common_1.getMissingProperties)(what, schema, required);
178
179
  if (missingProps.length > 0) {
179
180
  return (0, exports.makeValidationError)({
180
- code: types_1.ValidationErrorCode.MissingProperties,
181
+ code: types_2.ValidationErrorCode.MissingProperties,
181
182
  errors: Object.fromEntries(missingProps
182
183
  .map((error) => [
183
184
  error,
@@ -191,20 +192,20 @@ const validateWholeness = (what, schema) => {
191
192
  exports.validateWholeness = validateWholeness;
192
193
  const validate = (what, schema, options = {}) => {
193
194
  if (!what) {
194
- return common_1.Result.error((0, exports.makeValidationError)({
195
- code: types_1.ValidationErrorCode.EmptyTarget,
195
+ return types_1.Result.error((0, exports.makeValidationError)({
196
+ code: types_2.ValidationErrorCode.EmptyTarget,
196
197
  errors: {},
197
198
  }));
198
199
  }
199
200
  if (!('properties' in schema)) {
200
201
  const { error } = (0, exports.validateProperty)('', what, schema);
201
202
  return error
202
- ? common_1.Result.error(error)
203
- : common_1.Result.result(what);
203
+ ? types_1.Result.error(error)
204
+ : types_1.Result.result(what);
204
205
  }
205
206
  const wholenessError = (0, exports.validateWholeness)(what, schema);
206
207
  if (wholenessError) {
207
- return common_1.Result.error(wholenessError);
208
+ return types_1.Result.error(wholenessError);
208
209
  }
209
210
  const errors = {};
210
211
  const resultCopy = {};
@@ -219,18 +220,18 @@ const validate = (what, schema, options = {}) => {
219
220
  }
220
221
  if (Object.keys(errors).length > 0) {
221
222
  if (options.throwOnError) {
222
- const error = new TypeError(types_1.ValidationErrorCode.InvalidProperties);
223
+ const error = new TypeError(types_2.ValidationErrorCode.InvalidProperties);
223
224
  Object.assign(error, {
224
225
  errors,
225
226
  });
226
227
  throw error;
227
228
  }
228
- return common_1.Result.error((0, exports.makeValidationError)({
229
- code: types_1.ValidationErrorCode.InvalidProperties,
229
+ return types_1.Result.error((0, exports.makeValidationError)({
230
+ code: types_2.ValidationErrorCode.InvalidProperties,
230
231
  errors,
231
232
  }));
232
233
  }
233
- return common_1.Result.result(resultCopy);
234
+ return types_1.Result.result(resultCopy);
234
235
  };
235
236
  exports.validate = validate;
236
237
  const validator = (schema, options = {}) => {
package/dist/validate.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
- import { Result, getMissingProperties } from "@aeriajs/common";
2
+ import { Result } from "@aeriajs/types";
3
+ import { getMissingProperties } from "@aeriajs/common";
3
4
  import { ValidationErrorCode, PropertyValidationErrorCode } from "@aeriajs/types";
4
5
  const getValueType = (value) => {
5
6
  return Array.isArray(value) ? "array" : typeof value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/validation",
3
- "version": "0.0.71",
3
+ "version": "0.0.72",
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.68",
30
- "@aeriajs/types": "^0.0.60"
29
+ "@aeriajs/common": "^0.0.69",
30
+ "@aeriajs/types": "^0.0.61"
31
31
  },
32
32
  "scripts": {
33
33
  "test": "env TS_NODE_COMPILER_OPTIONS=\"$(cat ../compilerOptions.json)\" mocha -r ts-node/register tests/*.spec.ts",