@aeriajs/validation 0.0.124 → 0.0.125

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.
package/dist/validate.js CHANGED
@@ -12,11 +12,10 @@ const getValueType = (value) => {
12
12
  const getPropertyType = (property) => {
13
13
  if ('type' in property) {
14
14
  if ('format' in property && property.format) {
15
- if ([
16
- 'date',
17
- 'date-time',
18
- ].includes(property.format)) {
19
- return 'datetime';
15
+ switch (property.format) {
16
+ case 'date':
17
+ case 'date-time':
18
+ return 'datetime';
20
19
  }
21
20
  }
22
21
  return property.type;
@@ -24,6 +23,9 @@ const getPropertyType = (property) => {
24
23
  if ('enum' in property) {
25
24
  return typeof property.enum[0];
26
25
  }
26
+ if ('const' in property) {
27
+ return typeof property.const;
28
+ }
27
29
  if ('$ref' in property
28
30
  || 'properties' in property
29
31
  || 'additionalProperties' in property) {
@@ -56,34 +58,19 @@ const validateProperty = (propName, what, property, options = {}) => {
56
58
  if (what === null || what === undefined) {
57
59
  return types_1.Result.result(what);
58
60
  }
59
- if ('properties' in property) {
60
- return (0, exports.validate)(what, property, options);
61
- }
62
- if ('const' in property) {
63
- if (what !== property.const) {
64
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
65
- expected: property.const,
66
- got: what,
67
- }));
68
- }
69
- return types_1.Result.result(what);
70
- }
71
- if ('enum' in property && property.enum.length === 0) {
72
- return types_1.Result.result(what);
73
- }
74
61
  if ('getter' in property) {
75
62
  return types_1.Result.result(undefined);
76
63
  }
77
64
  const expectedType = getPropertyType(property);
78
65
  const actualType = getValueType(what);
79
66
  if (actualType !== expectedType
80
- && !('items' in property && actualType === 'array')
67
+ && !(('items' in property || 'enum' in property) && actualType === 'array')
81
68
  && !(actualType === 'number' && expectedType === 'integer')) {
82
69
  if (expectedType === 'datetime' && what instanceof Date) {
83
70
  return types_1.Result.result(what);
84
71
  }
85
72
  if ('$ref' in property && typeof what === 'string') {
86
- if (/^[0-9a-fA-F]{24}$/.test(what)) {
73
+ if (/^[0-9a-f]{24}$/.test(what)) {
87
74
  return types_1.Result.result(what);
88
75
  }
89
76
  }
@@ -109,6 +96,21 @@ const validateProperty = (propName, what, property, options = {}) => {
109
96
  got: actualType,
110
97
  }));
111
98
  }
99
+ if ('properties' in property) {
100
+ return (0, exports.validate)(what, property, options);
101
+ }
102
+ if ('enum' in property && property.enum.length === 0) {
103
+ return types_1.Result.result(what);
104
+ }
105
+ if ('const' in property) {
106
+ if (what !== property.const) {
107
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
108
+ expected: property.const,
109
+ got: what,
110
+ }));
111
+ }
112
+ return types_1.Result.result(what);
113
+ }
112
114
  if ('items' in property) {
113
115
  if (!Array.isArray(what)) {
114
116
  return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
@@ -140,29 +142,32 @@ const validateProperty = (propName, what, property, options = {}) => {
140
142
  }
141
143
  }
142
144
  else if ('type' in property) {
143
- if (property.type === 'integer') {
144
- if (!Number.isInteger(what)) {
145
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
146
- expected: 'integer',
147
- got: 'invalid_number',
148
- }));
149
- }
150
- }
151
- if (property.type === 'integer' || property.type === 'number') {
152
- if (typeof what !== 'number') {
153
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
154
- expected: expectedType,
155
- got: actualType,
156
- }));
157
- }
158
- if ((property.maximum && property.maximum < what)
159
- || (property.minimum && property.minimum > what)
160
- || (property.exclusiveMaximum && property.exclusiveMaximum <= what)
161
- || (property.exclusiveMinimum && property.exclusiveMinimum >= what)) {
162
- return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
163
- expected: 'number',
164
- got: 'invalid_number',
165
- }));
145
+ switch (property.type) {
146
+ case 'number':
147
+ case 'integer': {
148
+ if (typeof what !== 'number') {
149
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Unmatching, {
150
+ expected: expectedType,
151
+ got: actualType,
152
+ }));
153
+ }
154
+ if (property.type === 'integer') {
155
+ if (!Number.isInteger(what)) {
156
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
157
+ expected: 'integer',
158
+ got: 'invalid_number',
159
+ }));
160
+ }
161
+ }
162
+ if ((property.maximum && property.maximum < what)
163
+ || (property.minimum && property.minimum > what)
164
+ || (property.exclusiveMaximum && property.exclusiveMaximum <= what)
165
+ || (property.exclusiveMinimum && property.exclusiveMinimum >= what)) {
166
+ return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.NumericConstraint, {
167
+ expected: 'number',
168
+ got: 'invalid_number',
169
+ }));
170
+ }
166
171
  }
167
172
  }
168
173
  }
package/dist/validate.mjs CHANGED
@@ -8,11 +8,10 @@ const getValueType = (value) => {
8
8
  const getPropertyType = (property) => {
9
9
  if ("type" in property) {
10
10
  if ("format" in property && property.format) {
11
- if ([
12
- "date",
13
- "date-time"
14
- ].includes(property.format)) {
15
- return "datetime";
11
+ switch (property.format) {
12
+ case "date":
13
+ case "date-time":
14
+ return "datetime";
16
15
  }
17
16
  }
18
17
  return property.type;
@@ -20,6 +19,9 @@ const getPropertyType = (property) => {
20
19
  if ("enum" in property) {
21
20
  return typeof property.enum[0];
22
21
  }
22
+ if ("const" in property) {
23
+ return typeof property.const;
24
+ }
23
25
  if ("$ref" in property || "properties" in property || "additionalProperties" in property) {
24
26
  return "object";
25
27
  }
@@ -49,32 +51,17 @@ export const validateProperty = (propName, what, property, options = {}) => {
49
51
  if (what === null || what === void 0) {
50
52
  return Result.result(what);
51
53
  }
52
- if ("properties" in property) {
53
- return validate(what, property, options);
54
- }
55
- if ("const" in property) {
56
- if (what !== property.const) {
57
- return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
58
- expected: property.const,
59
- got: what
60
- }));
61
- }
62
- return Result.result(what);
63
- }
64
- if ("enum" in property && property.enum.length === 0) {
65
- return Result.result(what);
66
- }
67
54
  if ("getter" in property) {
68
55
  return Result.result(void 0);
69
56
  }
70
57
  const expectedType = getPropertyType(property);
71
58
  const actualType = getValueType(what);
72
- if (actualType !== expectedType && !("items" in property && actualType === "array") && !(actualType === "number" && expectedType === "integer")) {
59
+ if (actualType !== expectedType && !(("items" in property || "enum" in property) && actualType === "array") && !(actualType === "number" && expectedType === "integer")) {
73
60
  if (expectedType === "datetime" && what instanceof Date) {
74
61
  return Result.result(what);
75
62
  }
76
63
  if ("$ref" in property && typeof what === "string") {
77
- if (/^[0-9a-fA-F]{24}$/.test(what)) {
64
+ if (/^[0-9a-f]{24}$/.test(what)) {
78
65
  return Result.result(what);
79
66
  }
80
67
  }
@@ -100,6 +87,21 @@ export const validateProperty = (propName, what, property, options = {}) => {
100
87
  got: actualType
101
88
  }));
102
89
  }
90
+ if ("properties" in property) {
91
+ return validate(what, property, options);
92
+ }
93
+ if ("enum" in property && property.enum.length === 0) {
94
+ return Result.result(what);
95
+ }
96
+ if ("const" in property) {
97
+ if (what !== property.const) {
98
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
99
+ expected: property.const,
100
+ got: what
101
+ }));
102
+ }
103
+ return Result.result(what);
104
+ }
103
105
  if ("items" in property) {
104
106
  if (!Array.isArray(what)) {
105
107
  return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
@@ -130,26 +132,29 @@ export const validateProperty = (propName, what, property, options = {}) => {
130
132
  i++;
131
133
  }
132
134
  } else if ("type" in property) {
133
- if (property.type === "integer") {
134
- if (!Number.isInteger(what)) {
135
- return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
136
- expected: "integer",
137
- got: "invalid_number"
138
- }));
139
- }
140
- }
141
- if (property.type === "integer" || property.type === "number") {
142
- if (typeof what !== "number") {
143
- return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
144
- expected: expectedType,
145
- got: actualType
146
- }));
147
- }
148
- if (property.maximum && property.maximum < what || property.minimum && property.minimum > what || property.exclusiveMaximum && property.exclusiveMaximum <= what || property.exclusiveMinimum && property.exclusiveMinimum >= what) {
149
- return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
150
- expected: "number",
151
- got: "invalid_number"
152
- }));
135
+ switch (property.type) {
136
+ case "number":
137
+ case "integer": {
138
+ if (typeof what !== "number") {
139
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
140
+ expected: expectedType,
141
+ got: actualType
142
+ }));
143
+ }
144
+ if (property.type === "integer") {
145
+ if (!Number.isInteger(what)) {
146
+ return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
147
+ expected: "integer",
148
+ got: "invalid_number"
149
+ }));
150
+ }
151
+ }
152
+ if (property.maximum && property.maximum < what || property.minimum && property.minimum > what || property.exclusiveMaximum && property.exclusiveMaximum <= what || property.exclusiveMinimum && property.exclusiveMinimum >= what) {
153
+ return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
154
+ expected: "number",
155
+ got: "invalid_number"
156
+ }));
157
+ }
153
158
  }
154
159
  }
155
160
  } else if ("enum" in property) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/validation",
3
- "version": "0.0.124",
3
+ "version": "0.0.125",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -26,7 +26,7 @@
26
26
  "@aeriajs/types": "link:../types"
27
27
  },
28
28
  "peerDependencies": {
29
- "@aeriajs/common": "^0.0.119",
29
+ "@aeriajs/common": "^0.0.120",
30
30
  "@aeriajs/types": "^0.0.102",
31
31
  "mongodb": "^6.5.0"
32
32
  },