ascertain 0.12.33 → 0.13.1
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.d.ts +2 -0
- package/build/index.js +187 -175
- 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.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ export function or<T = any>(...schemas: Schema<T>[]): Schema<any>;
|
|
|
16
16
|
interface As {
|
|
17
17
|
string: (value: string | undefined) => string,
|
|
18
18
|
number: (value: string | undefined) => number,
|
|
19
|
+
date: (value: string | undefined) => Date,
|
|
20
|
+
time: (value: string | undefined) => number,
|
|
19
21
|
boolean: (value: string | undefined) => boolean,
|
|
20
22
|
array: (value: string | undefined, delimiter: string | RegExp) => string[];
|
|
21
23
|
json: <T>(value: string | undefined) => T;
|
package/build/index.js
CHANGED
|
@@ -1,209 +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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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;
|
|
26
46
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
47
|
+
toString() {
|
|
48
|
+
return `Invalid ${this.subject} ${JSON.stringify(this.value)} specified by path ${this.path} expected ${this.expected}`;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
30
51
|
function findFirstError(array, map) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
52
|
+
for(let i = 0; i < array.length; i++){
|
|
53
|
+
const error = map(array[i], i);
|
|
54
|
+
if (error) {
|
|
55
|
+
return error;
|
|
56
|
+
}
|
|
35
57
|
}
|
|
36
|
-
|
|
37
|
-
return false;
|
|
58
|
+
return false;
|
|
38
59
|
}
|
|
39
60
|
function findNotError(array, map) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
}
|
|
47
69
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
+
});
|
|
52
76
|
}
|
|
53
77
|
function Optional(schema) {
|
|
54
|
-
|
|
78
|
+
this.schema = schema;
|
|
55
79
|
}
|
|
56
80
|
function And(schema) {
|
|
57
|
-
|
|
81
|
+
this.schema = schema;
|
|
58
82
|
}
|
|
59
83
|
function Or(schema) {
|
|
60
|
-
|
|
84
|
+
this.schema = schema;
|
|
61
85
|
}
|
|
62
|
-
|
|
63
|
-
|
|
86
|
+
const optional = (schema)=>{
|
|
87
|
+
return new Optional(schema);
|
|
64
88
|
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
schema[_key] = arguments[_key];
|
|
68
|
-
}
|
|
69
|
-
return new And(schema);
|
|
89
|
+
const and = (...schema)=>{
|
|
90
|
+
return new And(schema);
|
|
70
91
|
};
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
schema[_key2] = arguments[_key2];
|
|
74
|
-
}
|
|
75
|
-
return new Or(schema);
|
|
92
|
+
const or = (...schema)=>{
|
|
93
|
+
return new Or(schema);
|
|
76
94
|
};
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
|
83
105
|
};
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
+
}
|
|
111
141
|
}
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
var toggle = exports.toggle = function toggle(key, cases, defaultKey) {
|
|
115
|
-
var _ref, _cases$key;
|
|
116
|
-
return (_ref = (_cases$key = cases[key]) !== null && _cases$key !== void 0 ? _cases$key : cases[defaultKey]) !== null && _ref !== void 0 ? _ref : undefined;
|
|
117
142
|
};
|
|
118
143
|
function certain(target, schema, path, optional) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
if (schema instanceof Optional) {
|
|
123
|
-
return certain(target, schema.schema, path, true);
|
|
124
|
-
}
|
|
125
|
-
var isValue = target !== null && typeof target !== 'undefined';
|
|
126
|
-
if (!isValue && optional) {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
if (schema instanceof Or) {
|
|
130
|
-
if (!schema.schema.length) {
|
|
131
|
-
return new AssertError(target, 'values', path, 'OR schema');
|
|
132
|
-
}
|
|
133
|
-
return findNotError(schema.schema, function (schema) {
|
|
134
|
-
return certain(target, schema, path);
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
if (schema instanceof And) {
|
|
138
|
-
if (!schema.schema.length) {
|
|
139
|
-
return new AssertError(target, 'values', path, 'AND schema');
|
|
144
|
+
if (schema === null || typeof schema === 'undefined') {
|
|
145
|
+
return new AssertError(schema, 'any value', path, 'schema value');
|
|
140
146
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
if (typeof schema === 'function') {
|
|
146
|
-
if (!isValue) {
|
|
147
|
-
return new AssertError(target, schema.name, path);
|
|
147
|
+
if (schema instanceof Optional) {
|
|
148
|
+
return certain(target, schema.schema, path, true);
|
|
148
149
|
}
|
|
149
|
-
|
|
150
|
-
|
|
150
|
+
const isValue = target !== null && typeof target !== 'undefined';
|
|
151
|
+
if (!isValue && optional) {
|
|
152
|
+
return;
|
|
151
153
|
}
|
|
152
|
-
if (
|
|
153
|
-
|
|
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));
|
|
154
159
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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));
|
|
158
165
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (schema instanceof RegExp) {
|
|
166
|
-
if (!schema.test('' + target)) {
|
|
167
|
-
return new AssertError(target, "matching /".concat(schema.source, "/"), path);
|
|
168
|
-
}
|
|
169
|
-
} else {
|
|
170
|
-
if (_typeof(target) !== 'object') {
|
|
171
|
-
return new AssertError(target, schema.constructor.name, path);
|
|
172
|
-
}
|
|
173
|
-
if (target === null) {
|
|
174
|
-
return new AssertError(target, 'an object', path);
|
|
175
|
-
}
|
|
176
|
-
if ($keys in schema) {
|
|
177
|
-
var targetKeys = Object.keys(target);
|
|
178
|
-
var assertError = findFirstError(targetKeys, function (targetKey) {
|
|
179
|
-
return certain(targetKey, schema[$keys], "".concat(path, ".").concat(targetKey));
|
|
180
|
-
});
|
|
181
|
-
if (assertError) {
|
|
182
|
-
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);
|
|
183
172
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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}`));
|
|
189
182
|
});
|
|
190
|
-
|
|
191
|
-
|
|
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}`));
|
|
192
210
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
return certain(target[key], schema[key], "".concat(path, ".").concat(key));
|
|
196
|
-
});
|
|
211
|
+
} else if (target !== schema) {
|
|
212
|
+
return new AssertError(target, schema, path);
|
|
197
213
|
}
|
|
198
|
-
} else if (target !== schema) {
|
|
199
|
-
return new AssertError(target, schema, path);
|
|
200
|
-
}
|
|
201
214
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
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
|
+
}
|
|
208
220
|
};
|
|
209
|
-
|
|
221
|
+
const _default = ascertain;
|
package/package.json
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ascertain",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.1",
|
|
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 babel src --out-dir build --ignore **/__tests__,**/__fixtures__ && cp src/index.d.ts build",
|
|
9
|
-
"test": "NODE_ENV=test jest",
|
|
10
|
-
"test:cov": "COVERAGE=1 NODE_ENV=test jest"
|
|
11
|
-
},
|
|
12
7
|
"repository": {
|
|
13
8
|
"type": "git",
|
|
14
9
|
"url": "git+https://github.com/3axap4eHko/ascertain.git"
|
|
@@ -33,11 +28,14 @@
|
|
|
33
28
|
},
|
|
34
29
|
"homepage": "https://github.com/3axap4eHko/ascertain#readme",
|
|
35
30
|
"devDependencies": {
|
|
36
|
-
"@
|
|
37
|
-
"@
|
|
38
|
-
"@
|
|
39
|
-
"babel-jest": "^29.7.0",
|
|
31
|
+
"@swc/cli": "^0.3.10",
|
|
32
|
+
"@swc/core": "^1.4.8",
|
|
33
|
+
"@swc/jest": "^0.2.36",
|
|
40
34
|
"jest": "^29.7.0"
|
|
41
35
|
},
|
|
42
|
-
"
|
|
43
|
-
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "NODE_ENV=production swc src/index.js -d build --ignore **/__tests__,**/__fixtures__ --strip-leading-paths && cp src/index.d.ts build",
|
|
38
|
+
"test": "NODE_ENV=test jest",
|
|
39
|
+
"test:cov": "COVERAGE=1 NODE_ENV=test jest"
|
|
40
|
+
}
|
|
41
|
+
}
|