@aeriajs/validation 0.0.139 → 0.0.141

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,6 +1,5 @@
1
1
  import type { JsonSchema, Property, InferSchema, Description, PropertyValidationError, ValidationError } from '@aeriajs/types';
2
- import { Result } from '@aeriajs/types';
3
- import { ValidationErrorCode } from '@aeriajs/types';
2
+ import { Result, ValidationErrorCode } from '@aeriajs/types';
4
3
  export type ValidateOptions = {
5
4
  tolerateExtraneous?: boolean;
6
5
  throwOnError?: boolean;
package/dist/validate.js CHANGED
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validator = exports.validateWithRefs = exports.validate = exports.validateWholeness = exports.validateRefs = exports.validateProperty = exports.makeValidationError = void 0;
4
- const types_1 = require("@aeriajs/types");
5
4
  const common_1 = require("@aeriajs/common");
6
- const types_2 = require("@aeriajs/types");
5
+ const types_1 = require("@aeriajs/types");
7
6
  const entrypoint_1 = require("@aeriajs/entrypoint");
8
7
  const getPropertyType = (property) => {
9
8
  if ('type' in property) {
@@ -49,7 +48,7 @@ const validateProperty = (what, property, options = {}) => {
49
48
  if (options.tolerateExtraneous) {
50
49
  return types_1.Result.result(undefined);
51
50
  }
52
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Extraneous));
51
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Extraneous));
53
52
  }
54
53
  if (what === null || what === undefined) {
55
54
  return types_1.Result.result(what);
@@ -72,7 +71,7 @@ const validateProperty = (what, property, options = {}) => {
72
71
  if (/^[0-9a-f]{24}$/.test(what)) {
73
72
  return types_1.Result.result(what);
74
73
  }
75
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
74
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
76
75
  expected: expectedType,
77
76
  got: actualType,
78
77
  }));
@@ -96,7 +95,7 @@ const validateProperty = (what, property, options = {}) => {
96
95
  return types_1.Result.result(String(what));
97
96
  }
98
97
  }
99
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
98
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
100
99
  expected: expectedType,
101
100
  got: actualType,
102
101
  }));
@@ -105,7 +104,7 @@ const validateProperty = (what, property, options = {}) => {
105
104
  switch (property.type) {
106
105
  case 'integer': {
107
106
  if (!Number.isInteger(what)) {
108
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
107
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
109
108
  expected: 'integer',
110
109
  got: 'invalid_number',
111
110
  }));
@@ -113,7 +112,7 @@ const validateProperty = (what, property, options = {}) => {
113
112
  }
114
113
  case 'number': {
115
114
  if (typeof what !== 'number') {
116
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
115
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
117
116
  expected: expectedType,
118
117
  got: actualType,
119
118
  }));
@@ -122,7 +121,7 @@ const validateProperty = (what, property, options = {}) => {
122
121
  || (typeof property.minimum === 'number' && property.minimum > what)
123
122
  || (typeof property.exclusiveMaximum === 'number' && property.exclusiveMaximum <= what)
124
123
  || (typeof property.exclusiveMinimum === 'number' && property.exclusiveMinimum >= what)) {
125
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
124
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
126
125
  expected: 'number',
127
126
  got: 'invalid_number',
128
127
  }));
@@ -140,19 +139,19 @@ const validateProperty = (what, property, options = {}) => {
140
139
  }
141
140
  case 'array': {
142
141
  if (!Array.isArray(what)) {
143
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
142
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
144
143
  expected: expectedType,
145
144
  got: actualType,
146
145
  }));
147
146
  }
148
147
  if (property.minItems) {
149
148
  if (what.length < property.minItems) {
150
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.MoreItemsExpected));
149
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.MoreItemsExpected));
151
150
  }
152
151
  }
153
152
  if (property.maxItems) {
154
153
  if (what.length > property.maxItems) {
155
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.LessItemsExpected));
154
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.LessItemsExpected));
156
155
  }
157
156
  }
158
157
  let i = 0;
@@ -171,8 +170,8 @@ const validateProperty = (what, property, options = {}) => {
171
170
  }
172
171
  }
173
172
  else if ('enum' in property) {
174
- if (!property.enum.includes(what) && property.enum.length === 0) {
175
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.ExtraneousElement, {
173
+ if (!property.enum.includes(what)) {
174
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.ExtraneousElement, {
176
175
  expected: property.enum,
177
176
  got: what,
178
177
  }));
@@ -180,7 +179,7 @@ const validateProperty = (what, property, options = {}) => {
180
179
  }
181
180
  else if ('const' in property) {
182
181
  if (what !== property.const) {
183
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
182
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
184
183
  expected: property.const,
185
184
  got: what,
186
185
  }));
@@ -204,7 +203,7 @@ const validateRefs = async (what, property, options, descriptions) => {
204
203
  description = collection.description;
205
204
  }
206
205
  if (typeof what !== 'object') {
207
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
206
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
208
207
  expected: 'object',
209
208
  got: typeof what,
210
209
  }));
@@ -232,7 +231,7 @@ const validateRefs = async (what, property, options, descriptions) => {
232
231
  }
233
232
  if (Object.keys(errors).length > 0) {
234
233
  return types_1.Result.error((0, exports.makeValidationError)({
235
- code: types_2.ValidationErrorCode.InvalidProperties,
234
+ code: types_1.ValidationErrorCode.InvalidProperties,
236
235
  errors,
237
236
  }));
238
237
  }
@@ -248,7 +247,7 @@ const validateWholeness = (what, schema) => {
248
247
  const missingProps = (0, common_1.getMissingProperties)(what, schema, required);
249
248
  if (missingProps.length > 0) {
250
249
  return (0, exports.makeValidationError)({
251
- code: types_2.ValidationErrorCode.MissingProperties,
250
+ code: types_1.ValidationErrorCode.MissingProperties,
252
251
  errors: Object.fromEntries(missingProps.map((error) => [
253
252
  error,
254
253
  {
@@ -262,7 +261,7 @@ exports.validateWholeness = validateWholeness;
262
261
  const validate = (what, schema, options = {}) => {
263
262
  if (what === undefined) {
264
263
  return types_1.Result.error((0, exports.makeValidationError)({
265
- code: types_2.ValidationErrorCode.EmptyTarget,
264
+ code: types_1.ValidationErrorCode.EmptyTarget,
266
265
  errors: {},
267
266
  }));
268
267
  }
@@ -276,7 +275,7 @@ const validate = (what, schema, options = {}) => {
276
275
  const wholenessError = (0, exports.validateWholeness)(what, schema);
277
276
  if (wholenessError) {
278
277
  if (options.throwOnError) {
279
- throw new TypeError(types_2.ValidationErrorCode.MissingProperties);
278
+ throw new TypeError(types_1.ValidationErrorCode.MissingProperties);
280
279
  }
281
280
  return types_1.Result.error(wholenessError);
282
281
  }
@@ -289,7 +288,7 @@ const validate = (what, schema, options = {}) => {
289
288
  });
290
289
  if (error) {
291
290
  if (options.throwOnError) {
292
- throw new TypeError(types_2.ValidationErrorCode.InvalidProperties);
291
+ throw new TypeError(types_1.ValidationErrorCode.InvalidProperties);
293
292
  }
294
293
  errors[propName] = error;
295
294
  continue;
@@ -300,7 +299,7 @@ const validate = (what, schema, options = {}) => {
300
299
  }
301
300
  if (Object.keys(errors).length > 0) {
302
301
  return types_1.Result.error((0, exports.makeValidationError)({
303
- code: types_2.ValidationErrorCode.InvalidProperties,
302
+ code: types_1.ValidationErrorCode.InvalidProperties,
304
303
  errors,
305
304
  }));
306
305
  }
package/dist/validate.mjs CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
- import { Result } from "@aeriajs/types";
3
2
  import { getMissingProperties } from "@aeriajs/common";
4
- import { ValidationErrorCode, PropertyValidationErrorCode } from "@aeriajs/types";
3
+ import { Result, ValidationErrorCode, PropertyValidationErrorCode } from "@aeriajs/types";
5
4
  import { getCollection } from "@aeriajs/entrypoint";
6
5
  const getPropertyType = (property) => {
7
6
  if ("type" in property) {
@@ -161,7 +160,7 @@ export const validateProperty = (what, property, options = {}) => {
161
160
  }
162
161
  }
163
162
  } else if ("enum" in property) {
164
- if (!property.enum.includes(what) && property.enum.length === 0) {
163
+ if (!property.enum.includes(what)) {
165
164
  return Result.error(makePropertyError(PropertyValidationErrorCode.ExtraneousElement, {
166
165
  expected: property.enum,
167
166
  got: what
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/validation",
3
- "version": "0.0.139",
3
+ "version": "0.0.141",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -27,9 +27,9 @@
27
27
  "@aeriajs/types": "link:../types"
28
28
  },
29
29
  "peerDependencies": {
30
- "@aeriajs/common": "^0.0.125",
31
- "@aeriajs/entrypoint": "^0.0.128",
32
- "@aeriajs/types": "^0.0.107",
30
+ "@aeriajs/common": "^0.0.126",
31
+ "@aeriajs/entrypoint": "^0.0.129",
32
+ "@aeriajs/types": "^0.0.108",
33
33
  "mongodb": "^6.5.0"
34
34
  },
35
35
  "scripts": {