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 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
- value: true
3
+ value: true
5
4
  });
6
- exports.toggle = exports.or = exports.optional = exports.default = exports.ascertain = exports.as = exports.and = exports.$values = exports.$keys = void 0;
7
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
8
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
9
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
10
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
11
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
12
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
13
- var AssertError = /*#__PURE__*/function () {
14
- function AssertError(value, expected, path) {
15
- var subject = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'value';
16
- _classCallCheck(this, AssertError);
17
- this.value = value;
18
- this.expected = expected;
19
- this.path = path;
20
- this.subject = subject;
21
- }
22
- _createClass(AssertError, [{
23
- key: "toString",
24
- value: function toString() {
25
- return "Invalid ".concat(this.subject, " ").concat(JSON.stringify(this.value), " specified by path ").concat(this.path, " expected ").concat(this.expected);
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
- return AssertError;
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
- for (var i = 0; i < array.length; i++) {
32
- var error = map(array[i], i);
33
- if (error) {
34
- return error;
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
- var errors = [];
41
- for (var i = 0; i < array.length; i++) {
42
- var error = map(array[i], i);
43
- if (!error) {
44
- return false;
45
- } else {
46
- errors.push(error);
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
- return errors.reduce(function (result, error) {
50
- return new AssertError(result.value, [result.expected, error.expected].join(' or '), result.path, result.string);
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
- this.schema = schema;
78
+ this.schema = schema;
55
79
  }
56
80
  function And(schema) {
57
- this.schema = schema;
81
+ this.schema = schema;
58
82
  }
59
83
  function Or(schema) {
60
- this.schema = schema;
84
+ this.schema = schema;
61
85
  }
62
- var optional = exports.optional = function optional(schema) {
63
- return new Optional(schema);
86
+ const optional = (schema)=>{
87
+ return new Optional(schema);
64
88
  };
65
- var and = exports.and = function and() {
66
- for (var _len = arguments.length, schema = new Array(_len), _key = 0; _key < _len; _key++) {
67
- schema[_key] = arguments[_key];
68
- }
69
- return new And(schema);
89
+ const and = (...schema)=>{
90
+ return new And(schema);
70
91
  };
71
- var or = exports.or = function or() {
72
- for (var _len2 = arguments.length, schema = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
73
- schema[_key2] = arguments[_key2];
74
- }
75
- return new Or(schema);
92
+ const or = (...schema)=>{
93
+ return new Or(schema);
76
94
  };
77
- var $keys = exports.$keys = Symbol.for('@@keys');
78
- var $values = exports.$values = Symbol.for('@@values');
79
- var fromBase64 = typeof Buffer === 'undefined' ? function (value) {
80
- return atob(value);
81
- } : function (value) {
82
- return Buffer.from(value, 'base64').toString('utf-8');
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
- var as = exports.as = {
85
- string: function string(value) {
86
- return typeof value === "string" ? value : undefined;
87
- },
88
- number: function number(value) {
89
- var result = parseFloat(value);
90
- return Number.isFinite(result) ? result : undefined;
91
- },
92
- boolean: function boolean(value) {
93
- return /^(0|1|true|false|enabled|disabled)$/i.test(value) ? /^(1|true|enabled)$/i.test(value) : undefined;
94
- },
95
- array: function array(value, delimiter) {
96
- var _value$split;
97
- return (_value$split = value === null || value === void 0 ? void 0 : value.split(delimiter)) !== null && _value$split !== void 0 ? _value$split : undefined;
98
- },
99
- json: function json(value) {
100
- try {
101
- return JSON.parse(value);
102
- } catch (e) {
103
- return undefined;
104
- }
105
- },
106
- base64: function base64(value) {
107
- try {
108
- return fromBase64(value);
109
- } catch (e) {
110
- 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
+ }
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
- if (schema === null || typeof schema === 'undefined') {
120
- return new AssertError(schema, 'any value', path, 'schema value');
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
- return findFirstError(schema.schema, function (schema) {
142
- return certain(target, schema, path);
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
- if (_typeof(target) === 'object' && !(target instanceof schema)) {
150
- return new AssertError(target, schema.name, path);
150
+ const isValue = target !== null && typeof target !== 'undefined';
151
+ if (!isValue && optional) {
152
+ return;
151
153
  }
152
- if (_typeof(target) !== 'object' && target.constructor !== schema) {
153
- return new AssertError(target, schema.name, path);
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
- } else if (Array.isArray(schema)) {
156
- if (!Array.isArray(target)) {
157
- return new AssertError(target, schema.constructor.name, path);
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
- return findFirstError(target, function (value, idx) {
160
- return findNotError(schema, function (itemSchemaType) {
161
- return certain(value, itemSchemaType, "".concat(path, ".").concat(idx));
162
- });
163
- });
164
- } else if (_typeof(schema) === 'object') {
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
- if ($values in schema) {
186
- var _targetKeys = Object.keys(target);
187
- var _assertError = findFirstError(_targetKeys, function (targetKey) {
188
- return certain(target[targetKey], schema[$values], "".concat(path, ".").concat(targetKey));
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
- if (_assertError) {
191
- return _assertError;
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
- return findFirstError(Object.keys(schema), function (key) {
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
- var ascertain = exports.ascertain = function ascertain(schema, data) {
203
- var rootName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '[root]';
204
- var result = certain(data, schema, rootName);
205
- if (result instanceof AssertError) {
206
- throw new TypeError(result.toString());
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
- var _default = exports.default = ascertain;
221
+ const _default = ascertain;
package/package.json CHANGED
@@ -1,14 +1,9 @@
1
1
  {
2
2
  "name": "ascertain",
3
- "version": "0.12.33",
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
- "@babel/cli": "^7.23.9",
37
- "@babel/core": "^7.24.0",
38
- "@babel/preset-env": "^7.24.0",
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
- "dependencies": {}
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
+ }