@aeriajs/validation 0.0.62 → 0.0.63

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,4 +1,5 @@
1
- import type { Either, JsonSchema, Property, InferSchema, Description, PropertyValidationError, ValidationError } from '@aeriajs/types';
1
+ import type { JsonSchema, Property, InferSchema, Description, PropertyValidationError, ValidationError } from '@aeriajs/types';
2
+ import { Result } from '@aeriajs/common';
2
3
  import { ValidationErrorCode } from '@aeriajs/types';
3
4
  export type ValidateOptions = {
4
5
  extraneous?: string[] | boolean;
@@ -7,7 +8,7 @@ export type ValidateOptions = {
7
8
  coerce?: boolean;
8
9
  };
9
10
  export declare const makeValidationError: <TValidationError extends ValidationError>(error: TValidationError) => TValidationError;
10
- export declare const validateProperty: <TWhat>(propName: string, what: TWhat, property: Property | undefined, options?: ValidateOptions) => Either<PropertyValidationError | ValidationError, unknown>;
11
+ export declare const validateProperty: <TWhat>(propName: string, what: TWhat, property: Property | undefined, options?: ValidateOptions) => Result.Either<PropertyValidationError | ValidationError, unknown>;
11
12
  export declare const validateWholeness: (what: Record<string, any>, schema: Omit<JsonSchema, '$id'>) => {
12
13
  code: ValidationErrorCode.MissingProperties;
13
14
  errors: {
@@ -16,5 +17,21 @@ export declare const validateWholeness: (what: Record<string, any>, schema: Omit
16
17
  };
17
18
  };
18
19
  } | undefined;
19
- export declare const validate: <TWhat, const TJsonSchema extends Property | Omit<Description, "$id">>(what: TWhat | undefined, schema: TJsonSchema, options?: ValidateOptions) => import("@aeriajs/types").Left<PropertyValidationError | ValidationError> | import("@aeriajs/types").Right<InferSchema<TJsonSchema>>;
20
- export declare const validator: <const TJsonSchema extends Property | Omit<Description, "$id">>(schema: TJsonSchema, options?: ValidateOptions) => readonly [InferSchema<TJsonSchema>, <TWhat>(what: TWhat) => import("@aeriajs/types").Left<PropertyValidationError | ValidationError> | import("@aeriajs/types").Right<InferSchema<TJsonSchema>>];
20
+ export declare const validate: <TWhat, const TJsonSchema extends Property | Omit<Description, "$id">>(what: TWhat | undefined, schema: TJsonSchema, options?: ValidateOptions) => {
21
+ readonly _tag: "Error";
22
+ readonly error: PropertyValidationError | ValidationError;
23
+ readonly result: undefined;
24
+ } | {
25
+ readonly _tag: "Result";
26
+ readonly error: undefined;
27
+ readonly result: InferSchema<TJsonSchema>;
28
+ };
29
+ export declare const validator: <const TJsonSchema extends Property | Omit<Description, "$id">>(schema: TJsonSchema, options?: ValidateOptions) => readonly [InferSchema<TJsonSchema>, <TWhat>(what: TWhat) => {
30
+ readonly _tag: "Error";
31
+ readonly error: PropertyValidationError | ValidationError;
32
+ readonly result: undefined;
33
+ } | {
34
+ readonly _tag: "Result";
35
+ readonly error: undefined;
36
+ readonly result: InferSchema<TJsonSchema>;
37
+ }];
package/dist/validate.js CHANGED
@@ -42,93 +42,92 @@ exports.makeValidationError = makeValidationError;
42
42
  const validateProperty = (propName, what, property, options = {}) => {
43
43
  const { extraneous, filterOutExtraneous, coerce } = options;
44
44
  if (what === undefined) {
45
- return (0, common_1.right)(what);
45
+ return common_1.Result.result(what);
46
46
  }
47
47
  if (!property) {
48
48
  if (extraneous || (Array.isArray(extraneous) && extraneous.includes(propName))) {
49
49
  if (filterOutExtraneous) {
50
- return (0, common_1.right)(undefined);
50
+ return common_1.Result.result(undefined);
51
51
  }
52
- return (0, common_1.right)(what);
52
+ return common_1.Result.result(what);
53
53
  }
54
- return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.Extraneous, {
54
+ return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Extraneous, {
55
55
  expected: 'undefined',
56
56
  got: getValueType(what),
57
57
  }));
58
58
  }
59
59
  if ('getter' in property) {
60
- return (0, common_1.right)(undefined);
60
+ return common_1.Result.result(undefined);
61
61
  }
62
62
  if ('properties' in property) {
63
63
  return (0, exports.validate)(what, property, options);
64
64
  }
65
65
  if ('const' in property) {
66
66
  if (what !== property.const) {
67
- return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
67
+ return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
68
68
  expected: property.const,
69
69
  got: what,
70
70
  }));
71
71
  }
72
- return (0, common_1.right)(what);
72
+ return common_1.Result.result(what);
73
73
  }
74
74
  const expectedType = getPropertyType(property);
75
75
  const actualType = getValueType(what);
76
76
  if ('enum' in property && property.enum.length === 0) {
77
- return (0, common_1.right)(what);
77
+ return common_1.Result.result(what);
78
78
  }
79
79
  if (actualType !== expectedType
80
80
  && !('items' in property && actualType === 'array')
81
81
  && !(actualType === 'number' && expectedType === 'integer')) {
82
82
  if (expectedType === 'datetime' && what instanceof Date) {
83
- return (0, common_1.right)(what);
83
+ return common_1.Result.result(what);
84
84
  }
85
85
  if (expectedType === 'boolean' && !what) {
86
- return (0, common_1.right)(what);
86
+ return common_1.Result.result(what);
87
87
  }
88
88
  if ('$ref' in property && typeof what === 'string') {
89
89
  if (/^[0-9a-fA-F]{24}$/.test(what)) {
90
- return (0, common_1.right)(what);
90
+ return common_1.Result.result(what);
91
91
  }
92
92
  }
93
93
  if (coerce) {
94
94
  if (expectedType === 'number' && typeof what === 'string') {
95
95
  const coerced = parseFloat(what);
96
96
  if (!isNaN(coerced)) {
97
- return (0, common_1.right)(coerced);
97
+ return common_1.Result.result(coerced);
98
98
  }
99
99
  }
100
100
  if (expectedType === 'integer' && typeof what === 'string') {
101
101
  const coerced = parseInt(what);
102
102
  if (!isNaN(coerced)) {
103
- return (0, common_1.right)(coerced);
103
+ return common_1.Result.result(coerced);
104
104
  }
105
105
  }
106
106
  if (expectedType === 'string' && typeof what === 'number') {
107
- return (0, common_1.right)(String(what));
107
+ return common_1.Result.result(String(what));
108
108
  }
109
109
  }
110
- return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
110
+ return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
111
111
  expected: expectedType,
112
112
  got: actualType,
113
113
  }));
114
114
  }
115
115
  if ('items' in property) {
116
116
  if (!Array.isArray(what)) {
117
- return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
117
+ return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
118
118
  expected: expectedType,
119
119
  got: actualType,
120
120
  }));
121
121
  }
122
122
  let i = 0;
123
123
  for (const elem of what) {
124
- const resultEither = (0, exports.validateProperty)(propName, elem, property.items, options);
125
- if ((0, common_1.isLeft)(resultEither)) {
126
- const result = (0, common_1.unwrapEither)(resultEither);
127
- if ('errors' in result) {
124
+ const { error } = (0, exports.validateProperty)(propName, elem, property.items, options);
125
+ if (error) {
126
+ if ('errors' in error) {
128
127
  continue;
129
128
  }
130
- result.index = i;
131
- return (0, common_1.left)(result);
129
+ error.index = i;
130
+ return common_1.Result.error(error);
132
131
  }
133
132
  i++;
134
133
  }
@@ -136,7 +135,7 @@ const validateProperty = (propName, what, property, options = {}) => {
136
135
  else if ('type' in property) {
137
136
  if (property.type === 'integer') {
138
137
  if (!Number.isInteger(what)) {
139
- return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
138
+ return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
140
139
  expected: 'integer',
141
140
  got: 'invalid_number',
142
141
  }));
@@ -144,7 +143,7 @@ const validateProperty = (propName, what, property, options = {}) => {
144
143
  }
145
144
  if (property.type === 'integer' || property.type === 'number') {
146
145
  if (typeof what !== 'number') {
147
- return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
146
+ return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
148
147
  expected: expectedType,
149
148
  got: actualType,
150
149
  }));
@@ -153,7 +152,7 @@ const validateProperty = (propName, what, property, options = {}) => {
153
152
  || (property.minimum && property.minimum > what)
154
153
  || (property.exclusiveMaximum && property.exclusiveMaximum <= what)
155
154
  || (property.exclusiveMinimum && property.exclusiveMinimum >= what)) {
156
- return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
155
+ return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
157
156
  expected: 'number',
158
157
  got: 'invalid_number',
159
158
  }));
@@ -162,13 +161,13 @@ const validateProperty = (propName, what, property, options = {}) => {
162
161
  }
163
162
  else if ('enum' in property) {
164
163
  if (!property.enum.includes(what)) {
165
- return (0, common_1.left)(makePropertyError(types_1.PropertyValidationErrorCode.ExtraneousElement, {
164
+ return common_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.ExtraneousElement, {
166
165
  expected: property.enum,
167
166
  got: what,
168
167
  }));
169
168
  }
170
169
  }
171
- return (0, common_1.right)(what);
170
+ return common_1.Result.result(what);
172
171
  };
173
172
  exports.validateProperty = validateProperty;
174
173
  const validateWholeness = (what, schema) => {
@@ -192,30 +191,28 @@ const validateWholeness = (what, schema) => {
192
191
  exports.validateWholeness = validateWholeness;
193
192
  const validate = (what, schema, options = {}) => {
194
193
  if (!what) {
195
- return (0, common_1.left)((0, exports.makeValidationError)({
194
+ return common_1.Result.error((0, exports.makeValidationError)({
196
195
  code: types_1.ValidationErrorCode.EmptyTarget,
197
196
  errors: {},
198
197
  }));
199
198
  }
200
199
  if (!('properties' in schema)) {
201
- const resultEither = (0, exports.validateProperty)('', what, schema);
202
- return (0, common_1.isLeft)(resultEither)
203
- ? resultEither
204
- : (0, common_1.right)(what);
200
+ const { error } = (0, exports.validateProperty)('', what, schema);
201
+ return error
202
+ ? common_1.Result.error(error)
203
+ : common_1.Result.result(what);
205
204
  }
206
205
  const wholenessError = (0, exports.validateWholeness)(what, schema);
207
206
  if (wholenessError) {
208
- return (0, common_1.left)(wholenessError);
207
+ return common_1.Result.error(wholenessError);
209
208
  }
210
209
  const errors = {};
211
210
  const resultCopy = {};
212
211
  for (const propName in what) {
213
- const resultEither = (0, exports.validateProperty)(propName, what[propName], schema.properties[propName], options);
214
- if ((0, common_1.isLeft)(resultEither)) {
215
- const result = (0, common_1.unwrapEither)(resultEither);
216
- errors[propName] = result;
212
+ const { error, result: parsed } = (0, exports.validateProperty)(propName, what[propName], schema.properties[propName], options);
213
+ if (error) {
214
+ errors[propName] = error;
217
215
  }
218
- const parsed = (0, common_1.unwrapEither)(resultEither);
219
216
  if (parsed !== undefined) {
220
217
  resultCopy[propName] = parsed;
221
218
  }
@@ -228,12 +225,12 @@ const validate = (what, schema, options = {}) => {
228
225
  });
229
226
  throw error;
230
227
  }
231
- return (0, common_1.left)((0, exports.makeValidationError)({
228
+ return common_1.Result.error((0, exports.makeValidationError)({
232
229
  code: types_1.ValidationErrorCode.InvalidProperties,
233
230
  errors,
234
231
  }));
235
232
  }
236
- return (0, common_1.right)(resultCopy);
233
+ return common_1.Result.result(resultCopy);
237
234
  };
238
235
  exports.validate = validate;
239
236
  const validator = (schema, options = {}) => {
package/dist/validate.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- import { isLeft, left, right, unwrapEither, getMissingProperties } from "@aeriajs/common";
2
+ import { Result, getMissingProperties } from "@aeriajs/common";
3
3
  import { ValidationErrorCode, PropertyValidationErrorCode } from "@aeriajs/types";
4
4
  const getValueType = (value) => {
5
5
  return Array.isArray(value) ? "array" : typeof value;
@@ -35,98 +35,97 @@ export const makeValidationError = (error) => {
35
35
  export const validateProperty = (propName, what, property, options = {}) => {
36
36
  const { extraneous, filterOutExtraneous, coerce } = options;
37
37
  if (what === void 0) {
38
- return right(what);
38
+ return Result.result(what);
39
39
  }
40
40
  if (!property) {
41
41
  if (extraneous || Array.isArray(extraneous) && extraneous.includes(propName)) {
42
42
  if (filterOutExtraneous) {
43
- return right(void 0);
43
+ return Result.result(void 0);
44
44
  }
45
- return right(what);
45
+ return Result.result(what);
46
46
  }
47
- return left(makePropertyError(PropertyValidationErrorCode.Extraneous, {
47
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Extraneous, {
48
48
  expected: "undefined",
49
49
  got: getValueType(what)
50
50
  }));
51
51
  }
52
52
  if ("getter" in property) {
53
- return right(void 0);
53
+ return Result.result(void 0);
54
54
  }
55
55
  if ("properties" in property) {
56
56
  return validate(what, property, options);
57
57
  }
58
58
  if ("const" in property) {
59
59
  if (what !== property.const) {
60
- return left(makePropertyError(PropertyValidationErrorCode.Unmatching, {
60
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
61
61
  expected: property.const,
62
62
  got: what
63
63
  }));
64
64
  }
65
- return right(what);
65
+ return Result.result(what);
66
66
  }
67
67
  const expectedType = getPropertyType(property);
68
68
  const actualType = getValueType(what);
69
69
  if ("enum" in property && property.enum.length === 0) {
70
- return right(what);
70
+ return Result.result(what);
71
71
  }
72
72
  if (actualType !== expectedType && !("items" in property && actualType === "array") && !(actualType === "number" && expectedType === "integer")) {
73
73
  if (expectedType === "datetime" && what instanceof Date) {
74
- return right(what);
74
+ return Result.result(what);
75
75
  }
76
76
  if (expectedType === "boolean" && !what) {
77
- return right(what);
77
+ return Result.result(what);
78
78
  }
79
79
  if ("$ref" in property && typeof what === "string") {
80
80
  if (/^[0-9a-fA-F]{24}$/.test(what)) {
81
- return right(what);
81
+ return Result.result(what);
82
82
  }
83
83
  }
84
84
  if (coerce) {
85
85
  if (expectedType === "number" && typeof what === "string") {
86
86
  const coerced = parseFloat(what);
87
87
  if (!isNaN(coerced)) {
88
- return right(coerced);
88
+ return Result.result(coerced);
89
89
  }
90
90
  }
91
91
  if (expectedType === "integer" && typeof what === "string") {
92
92
  const coerced = parseInt(what);
93
93
  if (!isNaN(coerced)) {
94
- return right(coerced);
94
+ return Result.result(coerced);
95
95
  }
96
96
  }
97
97
  if (expectedType === "string" && typeof what === "number") {
98
- return right(String(what));
98
+ return Result.result(String(what));
99
99
  }
100
100
  }
101
- return left(makePropertyError(PropertyValidationErrorCode.Unmatching, {
101
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
102
102
  expected: expectedType,
103
103
  got: actualType
104
104
  }));
105
105
  }
106
106
  if ("items" in property) {
107
107
  if (!Array.isArray(what)) {
108
- return left(makePropertyError(PropertyValidationErrorCode.Unmatching, {
108
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
109
109
  expected: expectedType,
110
110
  got: actualType
111
111
  }));
112
112
  }
113
113
  let i = 0;
114
114
  for (const elem of what) {
115
- const resultEither = validateProperty(propName, elem, property.items, options);
116
- if (isLeft(resultEither)) {
117
- const result = unwrapEither(resultEither);
118
- if ("errors" in result) {
115
+ const { error } = validateProperty(propName, elem, property.items, options);
116
+ if (error) {
117
+ if ("errors" in error) {
119
118
  continue;
120
119
  }
121
- result.index = i;
122
- return left(result);
120
+ error.index = i;
121
+ return Result.error(error);
123
122
  }
124
123
  i++;
125
124
  }
126
125
  } else if ("type" in property) {
127
126
  if (property.type === "integer") {
128
127
  if (!Number.isInteger(what)) {
129
- return left(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
128
+ return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
130
129
  expected: "integer",
131
130
  got: "invalid_number"
132
131
  }));
@@ -134,13 +133,13 @@ export const validateProperty = (propName, what, property, options = {}) => {
134
133
  }
135
134
  if (property.type === "integer" || property.type === "number") {
136
135
  if (typeof what !== "number") {
137
- return left(makePropertyError(PropertyValidationErrorCode.Unmatching, {
136
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
138
137
  expected: expectedType,
139
138
  got: actualType
140
139
  }));
141
140
  }
142
141
  if (property.maximum && property.maximum < what || property.minimum && property.minimum > what || property.exclusiveMaximum && property.exclusiveMaximum <= what || property.exclusiveMinimum && property.exclusiveMinimum >= what) {
143
- return left(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
142
+ return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
144
143
  expected: "number",
145
144
  got: "invalid_number"
146
145
  }));
@@ -148,13 +147,13 @@ export const validateProperty = (propName, what, property, options = {}) => {
148
147
  }
149
148
  } else if ("enum" in property) {
150
149
  if (!property.enum.includes(what)) {
151
- return left(makePropertyError(PropertyValidationErrorCode.ExtraneousElement, {
150
+ return Result.error(makePropertyError(PropertyValidationErrorCode.ExtraneousElement, {
152
151
  expected: property.enum,
153
152
  got: what
154
153
  }));
155
154
  }
156
155
  }
157
- return right(what);
156
+ return Result.result(what);
158
157
  };
159
158
  export const validateWholeness = (what, schema) => {
160
159
  const required = schema.required ? schema.required : Object.keys(schema.properties);
@@ -173,33 +172,31 @@ export const validateWholeness = (what, schema) => {
173
172
  };
174
173
  export const validate = (what, schema, options = {}) => {
175
174
  if (!what) {
176
- return left(makeValidationError({
175
+ return Result.error(makeValidationError({
177
176
  code: ValidationErrorCode.EmptyTarget,
178
177
  errors: {}
179
178
  }));
180
179
  }
181
180
  if (!("properties" in schema)) {
182
- const resultEither = validateProperty("", what, schema);
183
- return isLeft(resultEither) ? resultEither : right(what);
181
+ const { error } = validateProperty("", what, schema);
182
+ return error ? Result.error(error) : Result.result(what);
184
183
  }
185
184
  const wholenessError = validateWholeness(what, schema);
186
185
  if (wholenessError) {
187
- return left(wholenessError);
186
+ return Result.error(wholenessError);
188
187
  }
189
188
  const errors = {};
190
189
  const resultCopy = {};
191
190
  for (const propName in what) {
192
- const resultEither = validateProperty(
191
+ const { error, result: parsed } = validateProperty(
193
192
  propName,
194
193
  what[propName],
195
194
  schema.properties[propName],
196
195
  options
197
196
  );
198
- if (isLeft(resultEither)) {
199
- const result = unwrapEither(resultEither);
200
- errors[propName] = result;
197
+ if (error) {
198
+ errors[propName] = error;
201
199
  }
202
- const parsed = unwrapEither(resultEither);
203
200
  if (parsed !== void 0) {
204
201
  resultCopy[propName] = parsed;
205
202
  }
@@ -212,12 +209,12 @@ export const validate = (what, schema, options = {}) => {
212
209
  });
213
210
  throw error;
214
211
  }
215
- return left(makeValidationError({
212
+ return Result.error(makeValidationError({
216
213
  code: ValidationErrorCode.InvalidProperties,
217
214
  errors
218
215
  }));
219
216
  }
220
- return right(resultCopy);
217
+ return Result.result(resultCopy);
221
218
  };
222
219
  export const validator = (schema, options = {}) => {
223
220
  return [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/validation",
3
- "version": "0.0.62",
3
+ "version": "0.0.63",
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.59",
30
- "@aeriajs/types": "^0.0.55"
29
+ "@aeriajs/common": "^0.0.60",
30
+ "@aeriajs/types": "^0.0.56"
31
31
  },
32
32
  "scripts": {
33
33
  "test": "env TS_NODE_COMPILER_OPTIONS=\"$(cat ../compilerOptions.json)\" mocha -r ts-node/register tests/*.spec.ts",