@aeriajs/validation 0.0.132 → 0.0.134

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.
@@ -9,6 +9,7 @@ export type ValidateOptions = {
9
9
  };
10
10
  export declare const makeValidationError: <TValidationError extends ValidationError>(error: TValidationError) => TValidationError;
11
11
  export declare const validateProperty: <TWhat>(what: TWhat, property: Property | undefined, options?: ValidateOptions) => Result.Either<PropertyValidationError | ValidationError, unknown>;
12
+ export declare const validateRefs: <TWhat>(what: TWhat, property: Property | undefined, descriptions?: Record<string, Description>) => Promise<Result.Either<PropertyValidationError | ValidationError, unknown>>;
12
13
  export declare const validateWholeness: (what: Record<string, unknown>, schema: Omit<JsonSchema, "$id">) => {
13
14
  code: ValidationErrorCode.MissingProperties;
14
15
  errors: {
package/dist/validate.js CHANGED
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validator = exports.validate = exports.validateWholeness = exports.validateProperty = exports.makeValidationError = void 0;
3
+ exports.validator = exports.validate = exports.validateWholeness = exports.validateRefs = exports.validateProperty = exports.makeValidationError = void 0;
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 entrypoint_1 = require("@aeriajs/entrypoint");
7
8
  const getPropertyType = (property) => {
8
9
  if ('type' in property) {
9
10
  if ('format' in property && property.format) {
@@ -65,9 +66,17 @@ const validateProperty = (what, property, options = {}) => {
65
66
  if (expectedType === 'datetime' && what instanceof Date) {
66
67
  return types_1.Result.result(what);
67
68
  }
68
- if ('$ref' in property && typeof what === 'string') {
69
- if (/^[0-9a-f]{24}$/.test(what)) {
70
- return types_1.Result.result(what);
69
+ if ('$ref' in property) {
70
+ switch (typeof what) {
71
+ case 'string': {
72
+ if (/^[0-9a-f]{24}$/.test(what)) {
73
+ return types_1.Result.result(what);
74
+ }
75
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
76
+ expected: expectedType,
77
+ got: actualType,
78
+ }));
79
+ }
71
80
  }
72
81
  }
73
82
  if (options.coerce) {
@@ -180,6 +189,58 @@ const validateProperty = (what, property, options = {}) => {
180
189
  return types_1.Result.result(what);
181
190
  };
182
191
  exports.validateProperty = validateProperty;
192
+ const validateRefs = async (what, property, descriptions) => {
193
+ if (property) {
194
+ if ('$ref' in property) {
195
+ let description;
196
+ if (descriptions) {
197
+ description = descriptions[property.$ref];
198
+ }
199
+ else {
200
+ const collection = await (0, entrypoint_1.getCollection)(property.$ref);
201
+ if (!collection) {
202
+ throw new Error;
203
+ }
204
+ description = collection.description;
205
+ }
206
+ if (typeof what !== 'object') {
207
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
208
+ expected: 'object',
209
+ got: typeof what,
210
+ }));
211
+ }
212
+ return (0, exports.validate)(what, description);
213
+ }
214
+ else if ('items' in property) {
215
+ if (!Array.isArray(what)) {
216
+ throw new Error;
217
+ }
218
+ for (const elem of what) {
219
+ const { error } = await (0, exports.validateRefs)(elem, property.items, descriptions);
220
+ if (error) {
221
+ return types_1.Result.error(error);
222
+ }
223
+ }
224
+ }
225
+ else if ('properties' in property) {
226
+ const errors = {};
227
+ for (const propName in what) {
228
+ const { error } = await (0, exports.validateRefs)(what[propName], property.properties[propName], descriptions);
229
+ if (error) {
230
+ errors[propName] = error;
231
+ }
232
+ }
233
+ if (Object.keys(errors).length > 0) {
234
+ return types_1.Result.error((0, exports.makeValidationError)({
235
+ code: types_2.ValidationErrorCode.InvalidProperties,
236
+ errors,
237
+ }));
238
+ }
239
+ }
240
+ }
241
+ return types_1.Result.result({});
242
+ };
243
+ exports.validateRefs = validateRefs;
183
244
  const validateWholeness = (what, schema) => {
184
245
  const required = schema.required
185
246
  ? schema.required
package/dist/validate.mjs CHANGED
@@ -2,6 +2,7 @@
2
2
  import { Result } from "@aeriajs/types";
3
3
  import { getMissingProperties } from "@aeriajs/common";
4
4
  import { ValidationErrorCode, PropertyValidationErrorCode } from "@aeriajs/types";
5
+ import { getCollection } from "@aeriajs/entrypoint";
5
6
  const getPropertyType = (property) => {
6
7
  if ("type" in property) {
7
8
  if ("format" in property && property.format) {
@@ -57,9 +58,17 @@ export const validateProperty = (what, property, options = {}) => {
57
58
  if (expectedType === "datetime" && what instanceof Date) {
58
59
  return Result.result(what);
59
60
  }
60
- if ("$ref" in property && typeof what === "string") {
61
- if (/^[0-9a-f]{24}$/.test(what)) {
62
- return Result.result(what);
61
+ if ("$ref" in property) {
62
+ switch (typeof what) {
63
+ case "string": {
64
+ if (/^[0-9a-f]{24}$/.test(what)) {
65
+ return Result.result(what);
66
+ }
67
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
68
+ expected: expectedType,
69
+ got: actualType
70
+ }));
71
+ }
63
72
  }
64
73
  }
65
74
  if (options.coerce) {
@@ -168,6 +177,54 @@ export const validateProperty = (what, property, options = {}) => {
168
177
  }
169
178
  return Result.result(what);
170
179
  };
180
+ export const validateRefs = async (what, property, descriptions) => {
181
+ if (property) {
182
+ if ("$ref" in property) {
183
+ let description;
184
+ if (descriptions) {
185
+ description = descriptions[property.$ref];
186
+ } else {
187
+ const collection = await getCollection(property.$ref);
188
+ if (!collection) {
189
+ throw new Error();
190
+ }
191
+ description = collection.description;
192
+ }
193
+ if (typeof what !== "object") {
194
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
195
+ expected: "object",
196
+ got: typeof what
197
+ }));
198
+ }
199
+ return validate(what, description);
200
+ } else if ("items" in property) {
201
+ if (!Array.isArray(what)) {
202
+ throw new Error();
203
+ }
204
+ for (const elem of what) {
205
+ const { error } = await validateRefs(elem, property.items, descriptions);
206
+ if (error) {
207
+ return Result.error(error);
208
+ }
209
+ }
210
+ } else if ("properties" in property) {
211
+ const errors = {};
212
+ for (const propName in what) {
213
+ const { error } = await validateRefs(what[propName], property.properties[propName], descriptions);
214
+ if (error) {
215
+ errors[propName] = error;
216
+ }
217
+ }
218
+ if (Object.keys(errors).length > 0) {
219
+ return Result.error(makeValidationError({
220
+ code: ValidationErrorCode.InvalidProperties,
221
+ errors
222
+ }));
223
+ }
224
+ }
225
+ }
226
+ return Result.result({});
227
+ };
171
228
  export const validateWholeness = (what, schema) => {
172
229
  const required = schema.required ? schema.required : Object.keys(schema.properties);
173
230
  const missingProps = getMissingProperties(what, schema, required);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/validation",
3
- "version": "0.0.132",
3
+ "version": "0.0.134",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -23,11 +23,13 @@
23
23
  },
24
24
  "devDependencies": {
25
25
  "@aeriajs/common": "link:../common",
26
+ "@aeriajs/entrypoint": "link:../entrypoint",
26
27
  "@aeriajs/types": "link:../types"
27
28
  },
28
29
  "peerDependencies": {
29
- "@aeriajs/common": "^0.0.124",
30
- "@aeriajs/types": "^0.0.106",
30
+ "@aeriajs/common": "^0.0.125",
31
+ "@aeriajs/entrypoint": "^0.0.128",
32
+ "@aeriajs/types": "^0.0.107",
31
33
  "mongodb": "^6.5.0"
32
34
  },
33
35
  "scripts": {