ascertain 0.13.0 → 0.13.2
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/.swcrc +16 -0
- package/build/index.js +187 -201
- package/package.json +10 -12
package/.swcrc
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/swcrc",
|
|
3
|
+
"module": {
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"strict": true,
|
|
6
|
+
"strictMode": true
|
|
7
|
+
},
|
|
8
|
+
"jsc": {
|
|
9
|
+
"target": "es2022",
|
|
10
|
+
"parser": {
|
|
11
|
+
"syntax": "ecmascript"
|
|
12
|
+
},
|
|
13
|
+
"externalHelpers": false,
|
|
14
|
+
"keepClassNames": true
|
|
15
|
+
}
|
|
16
|
+
}
|
package/build/index.js
CHANGED
|
@@ -1,235 +1,221 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
2
|
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
|
|
3
|
+
value: true
|
|
5
4
|
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
function
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
$keys: function() {
|
|
13
|
+
return $keys;
|
|
14
|
+
},
|
|
15
|
+
$values: function() {
|
|
16
|
+
return $values;
|
|
17
|
+
},
|
|
18
|
+
and: function() {
|
|
19
|
+
return and;
|
|
20
|
+
},
|
|
21
|
+
as: function() {
|
|
22
|
+
return as;
|
|
23
|
+
},
|
|
24
|
+
ascertain: function() {
|
|
25
|
+
return ascertain;
|
|
26
|
+
},
|
|
27
|
+
default: function() {
|
|
28
|
+
return _default;
|
|
29
|
+
},
|
|
30
|
+
fromBase64: function() {
|
|
31
|
+
return fromBase64;
|
|
32
|
+
},
|
|
33
|
+
optional: function() {
|
|
34
|
+
return optional;
|
|
35
|
+
},
|
|
36
|
+
or: function() {
|
|
37
|
+
return or;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
let AssertError = class AssertError {
|
|
41
|
+
constructor(value, expected, path, subject = 'value'){
|
|
42
|
+
this.value = value;
|
|
43
|
+
this.expected = expected;
|
|
44
|
+
this.path = path;
|
|
45
|
+
this.subject = subject;
|
|
32
46
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
47
|
+
toString() {
|
|
48
|
+
return `Invalid ${this.subject} ${JSON.stringify(this.value)} specified by path ${this.path} expected ${this.expected}`;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
36
51
|
function findFirstError(array, map) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
52
|
+
for(let i = 0; i < array.length; i++){
|
|
53
|
+
const error = map(array[i], i);
|
|
54
|
+
if (error) {
|
|
55
|
+
return error;
|
|
56
|
+
}
|
|
41
57
|
}
|
|
42
|
-
|
|
43
|
-
return false;
|
|
58
|
+
return false;
|
|
44
59
|
}
|
|
45
60
|
function findNotError(array, map) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
const errors = [];
|
|
62
|
+
for(let i = 0; i < array.length; i++){
|
|
63
|
+
const error = map(array[i], i);
|
|
64
|
+
if (!error) {
|
|
65
|
+
return false;
|
|
66
|
+
} else {
|
|
67
|
+
errors.push(error);
|
|
68
|
+
}
|
|
53
69
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
70
|
+
return errors.reduce((result, error)=>{
|
|
71
|
+
return new AssertError(result.value, [
|
|
72
|
+
result.expected,
|
|
73
|
+
error.expected
|
|
74
|
+
].join(' or '), result.path, result.string);
|
|
75
|
+
});
|
|
58
76
|
}
|
|
59
77
|
function Optional(schema) {
|
|
60
|
-
|
|
78
|
+
this.schema = schema;
|
|
61
79
|
}
|
|
62
80
|
function And(schema) {
|
|
63
|
-
|
|
81
|
+
this.schema = schema;
|
|
64
82
|
}
|
|
65
83
|
function Or(schema) {
|
|
66
|
-
|
|
84
|
+
this.schema = schema;
|
|
67
85
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
};
|
|
71
|
-
var and = exports.and = function and() {
|
|
72
|
-
for (var _len = arguments.length, schema = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
73
|
-
schema[_key] = arguments[_key];
|
|
74
|
-
}
|
|
75
|
-
return new And(schema);
|
|
86
|
+
const optional = (schema)=>{
|
|
87
|
+
return new Optional(schema);
|
|
76
88
|
};
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
schema[_key2] = arguments[_key2];
|
|
80
|
-
}
|
|
81
|
-
return new Or(schema);
|
|
89
|
+
const and = (...schema)=>{
|
|
90
|
+
return new And(schema);
|
|
82
91
|
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
var fromBase64 = exports.fromBase64 = typeof Buffer === 'undefined' ? function (value) {
|
|
86
|
-
return atob(value);
|
|
87
|
-
} : function (value) {
|
|
88
|
-
return Buffer.from(value, 'base64').toString('utf-8');
|
|
92
|
+
const or = (...schema)=>{
|
|
93
|
+
return new Or(schema);
|
|
89
94
|
};
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
const $keys = Symbol.for('@@keys');
|
|
96
|
+
const $values = Symbol.for('@@values');
|
|
97
|
+
const fromBase64 = typeof Buffer === 'undefined' ? (value)=>atob(value) : (value)=>Buffer.from(value, 'base64').toString('utf-8');
|
|
98
|
+
const MULTIPLIERS = {
|
|
99
|
+
ms: 1,
|
|
100
|
+
s: 1000,
|
|
101
|
+
m: 60000,
|
|
102
|
+
h: 3600000,
|
|
103
|
+
d: 86400000,
|
|
104
|
+
w: 604800000
|
|
97
105
|
};
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return undefined;
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
base64: function base64(value) {
|
|
137
|
-
try {
|
|
138
|
-
return fromBase64(value);
|
|
139
|
-
} catch (e) {
|
|
140
|
-
return undefined;
|
|
106
|
+
const as = {
|
|
107
|
+
string: (value)=>{
|
|
108
|
+
return typeof value === 'string' ? value : undefined;
|
|
109
|
+
},
|
|
110
|
+
number: (value)=>{
|
|
111
|
+
const result = parseFloat(value);
|
|
112
|
+
return Number.isFinite(result) ? result : undefined;
|
|
113
|
+
},
|
|
114
|
+
date: (value)=>{
|
|
115
|
+
const result = Date.parse(value);
|
|
116
|
+
return Number.isFinite(result) ? new Date(result) : undefined;
|
|
117
|
+
},
|
|
118
|
+
time: (value)=>{
|
|
119
|
+
const matches = value?.match(/^(\d+)(ms|s|m|h|d|w)?$/);
|
|
120
|
+
if (matches) {
|
|
121
|
+
const [_, amount, unit = 'ms'] = matches;
|
|
122
|
+
return parseInt(amount, 10) * MULTIPLIERS[unit];
|
|
123
|
+
}
|
|
124
|
+
return undefined;
|
|
125
|
+
},
|
|
126
|
+
boolean: (value)=>/^(0|1|true|false|enabled|disabled)$/i.test(value) ? /^(1|true|enabled)$/i.test(value) : undefined,
|
|
127
|
+
array: (value, delimiter)=>value?.split(delimiter) ?? undefined,
|
|
128
|
+
json: (value)=>{
|
|
129
|
+
try {
|
|
130
|
+
return JSON.parse(value);
|
|
131
|
+
} catch (e) {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
base64: (value)=>{
|
|
136
|
+
try {
|
|
137
|
+
return fromBase64(value);
|
|
138
|
+
} catch (e) {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
141
|
}
|
|
142
|
-
}
|
|
143
142
|
};
|
|
144
143
|
function certain(target, schema, path, optional) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
if (schema instanceof Optional) {
|
|
149
|
-
return certain(target, schema.schema, path, true);
|
|
150
|
-
}
|
|
151
|
-
var isValue = target !== null && typeof target !== 'undefined';
|
|
152
|
-
if (!isValue && optional) {
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
if (schema instanceof Or) {
|
|
156
|
-
if (!schema.schema.length) {
|
|
157
|
-
return new AssertError(target, 'values', path, 'OR schema');
|
|
144
|
+
if (schema === null || typeof schema === 'undefined') {
|
|
145
|
+
return new AssertError(schema, 'any value', path, 'schema value');
|
|
158
146
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
if (schema instanceof And) {
|
|
164
|
-
if (!schema.schema.length) {
|
|
165
|
-
return new AssertError(target, 'values', path, 'AND schema');
|
|
147
|
+
if (schema instanceof Optional) {
|
|
148
|
+
return certain(target, schema.schema, path, true);
|
|
166
149
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
if (typeof schema === 'function') {
|
|
172
|
-
if (!isValue) {
|
|
173
|
-
return new AssertError(target, schema.name, path);
|
|
150
|
+
const isValue = target !== null && typeof target !== 'undefined';
|
|
151
|
+
if (!isValue && optional) {
|
|
152
|
+
return;
|
|
174
153
|
}
|
|
175
|
-
if (
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
154
|
+
if (schema instanceof Or) {
|
|
155
|
+
if (!schema.schema.length) {
|
|
156
|
+
return new AssertError(target, 'values', path, 'OR schema');
|
|
157
|
+
}
|
|
158
|
+
return findNotError(schema.schema, (schema)=>certain(target, schema, path));
|
|
180
159
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
160
|
+
if (schema instanceof And) {
|
|
161
|
+
if (!schema.schema.length) {
|
|
162
|
+
return new AssertError(target, 'values', path, 'AND schema');
|
|
163
|
+
}
|
|
164
|
+
return findFirstError(schema.schema, (schema)=>certain(target, schema, path));
|
|
184
165
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if (schema instanceof RegExp) {
|
|
192
|
-
if (!schema.test('' + target)) {
|
|
193
|
-
return new AssertError(target, "matching /".concat(schema.source, "/"), path);
|
|
194
|
-
}
|
|
195
|
-
} else {
|
|
196
|
-
if (_typeof(target) !== 'object') {
|
|
197
|
-
return new AssertError(target, schema.constructor.name, path);
|
|
198
|
-
}
|
|
199
|
-
if (target === null) {
|
|
200
|
-
return new AssertError(target, 'an object', path);
|
|
201
|
-
}
|
|
202
|
-
if ($keys in schema) {
|
|
203
|
-
var targetKeys = Object.keys(target);
|
|
204
|
-
var assertError = findFirstError(targetKeys, function (targetKey) {
|
|
205
|
-
return certain(targetKey, schema[$keys], "".concat(path, ".").concat(targetKey));
|
|
206
|
-
});
|
|
207
|
-
if (assertError) {
|
|
208
|
-
return assertError;
|
|
166
|
+
if (typeof schema === 'function') {
|
|
167
|
+
if (!isValue) {
|
|
168
|
+
return new AssertError(target, schema.name, path);
|
|
169
|
+
}
|
|
170
|
+
if (typeof target === 'object' && !(target instanceof schema)) {
|
|
171
|
+
return new AssertError(target, schema.name, path);
|
|
209
172
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
173
|
+
if (typeof target !== 'object' && target.constructor !== schema) {
|
|
174
|
+
return new AssertError(target, schema.name, path);
|
|
175
|
+
}
|
|
176
|
+
} else if (Array.isArray(schema)) {
|
|
177
|
+
if (!Array.isArray(target)) {
|
|
178
|
+
return new AssertError(target, schema.constructor.name, path);
|
|
179
|
+
}
|
|
180
|
+
return findFirstError(target, (value, idx)=>{
|
|
181
|
+
return findNotError(schema, (itemSchemaType)=>certain(value, itemSchemaType, `${path}.${idx}`));
|
|
215
182
|
});
|
|
216
|
-
|
|
217
|
-
|
|
183
|
+
} else if (typeof schema === 'object') {
|
|
184
|
+
if (schema instanceof RegExp) {
|
|
185
|
+
if (!schema.test('' + target)) {
|
|
186
|
+
return new AssertError(target, `matching /${schema.source}/`, path);
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
if (typeof target !== 'object') {
|
|
190
|
+
return new AssertError(target, schema.constructor.name, path);
|
|
191
|
+
}
|
|
192
|
+
if (target === null) {
|
|
193
|
+
return new AssertError(target, 'an object', path);
|
|
194
|
+
}
|
|
195
|
+
if ($keys in schema) {
|
|
196
|
+
const targetKeys = Object.keys(target);
|
|
197
|
+
const assertError = findFirstError(targetKeys, (targetKey)=>certain(targetKey, schema[$keys], `${path}.${targetKey}`));
|
|
198
|
+
if (assertError) {
|
|
199
|
+
return assertError;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if ($values in schema) {
|
|
203
|
+
const targetKeys = Object.keys(target);
|
|
204
|
+
const assertError = findFirstError(targetKeys, (targetKey)=>certain(target[targetKey], schema[$values], `${path}.${targetKey}`));
|
|
205
|
+
if (assertError) {
|
|
206
|
+
return assertError;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return findFirstError(Object.keys(schema), (key)=>certain(target[key], schema[key], `${path}.${key}`));
|
|
218
210
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return certain(target[key], schema[key], "".concat(path, ".").concat(key));
|
|
222
|
-
});
|
|
211
|
+
} else if (target !== schema) {
|
|
212
|
+
return new AssertError(target, schema, path);
|
|
223
213
|
}
|
|
224
|
-
} else if (target !== schema) {
|
|
225
|
-
return new AssertError(target, schema, path);
|
|
226
|
-
}
|
|
227
214
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
215
|
+
const ascertain = (schema, data, rootName = '[root]')=>{
|
|
216
|
+
const result = certain(data, schema, rootName);
|
|
217
|
+
if (result instanceof AssertError) {
|
|
218
|
+
throw new TypeError(result.toString());
|
|
219
|
+
}
|
|
234
220
|
};
|
|
235
|
-
|
|
221
|
+
const _default = ascertain;
|
package/package.json
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ascertain",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.2",
|
|
4
4
|
"description": "0-Deps, simple, fast, for browser and node js object schema validator",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "NODE_ENV=production swc src/index.js -d build --ignore **/__tests__,**/__fixtures__ --strip-leading-paths && cp src/index.d.ts build",
|
|
9
|
+
"test": "NODE_ENV=test jest",
|
|
10
|
+
"test:cov": "COVERAGE=1 NODE_ENV=test jest"
|
|
11
|
+
},
|
|
7
12
|
"repository": {
|
|
8
13
|
"type": "git",
|
|
9
14
|
"url": "git+https://github.com/3axap4eHko/ascertain.git"
|
|
@@ -28,16 +33,9 @@
|
|
|
28
33
|
},
|
|
29
34
|
"homepage": "https://github.com/3axap4eHko/ascertain#readme",
|
|
30
35
|
"devDependencies": {
|
|
31
|
-
"@
|
|
32
|
-
"@
|
|
33
|
-
"@
|
|
34
|
-
"babel-jest": "^29.7.0",
|
|
36
|
+
"@swc/cli": "^0.3.10",
|
|
37
|
+
"@swc/core": "^1.4.11",
|
|
38
|
+
"@swc/jest": "^0.2.36",
|
|
35
39
|
"jest": "^29.7.0"
|
|
36
|
-
},
|
|
37
|
-
"dependencies": {},
|
|
38
|
-
"scripts": {
|
|
39
|
-
"build": "NODE_ENV=production babel src --out-dir build --ignore **/__tests__,**/__fixtures__ && cp src/index.d.ts build",
|
|
40
|
-
"test": "NODE_ENV=test jest",
|
|
41
|
-
"test:cov": "COVERAGE=1 NODE_ENV=test jest"
|
|
42
40
|
}
|
|
43
|
-
}
|
|
41
|
+
}
|