ascertain 3.2.0 → 3.2.8
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/README.md +38 -7
- package/build/__bench__/benchmark.d.ts +1 -0
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +82 -0
- package/build/index.js.map +1 -1
- package/package.json +14 -14
- package/src/index.ts +82 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ascertain",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.8",
|
|
4
4
|
"description": "0-Deps, simple, fast, for browser and node js object schema validator",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -40,36 +40,36 @@
|
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@eslint/js": "^10.0.1",
|
|
42
42
|
"@types/node": "^25.5.0",
|
|
43
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
44
|
-
"@typescript-eslint/parser": "^8.
|
|
45
|
-
"@typescript-eslint/typescript-estree": "^8.
|
|
46
|
-
"@vitest/coverage-v8": "^4.1.
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "^8.58.0",
|
|
44
|
+
"@typescript-eslint/parser": "^8.58.0",
|
|
45
|
+
"@typescript-eslint/typescript-estree": "^8.58.0",
|
|
46
|
+
"@vitest/coverage-v8": "^4.1.2",
|
|
47
47
|
"@vuepress/bundler-vite": "2.0.0-rc.27",
|
|
48
|
-
"@vuepress/theme-default": "2.0.0-rc.
|
|
48
|
+
"@vuepress/theme-default": "2.0.0-rc.126",
|
|
49
49
|
"ajv": "^8.18.0",
|
|
50
50
|
"ascertain": "latest",
|
|
51
|
-
"eslint": "^10.0
|
|
51
|
+
"eslint": "^10.1.0",
|
|
52
52
|
"eslint-config-prettier": "^10.1.8",
|
|
53
53
|
"eslint-plugin-prettier": "^5.5.5",
|
|
54
|
-
"handlebars": "^4.7.
|
|
54
|
+
"handlebars": "^4.7.9",
|
|
55
55
|
"husky": "^9.1.7",
|
|
56
56
|
"inop": "^0.9.0",
|
|
57
|
-
"overtake": "^
|
|
57
|
+
"overtake": "^2.0.3",
|
|
58
58
|
"prettier": "^3.8.1",
|
|
59
59
|
"recast": "^0.23.11",
|
|
60
60
|
"sass-embedded": "^1.98.0",
|
|
61
61
|
"ts-node": "^10.9.2",
|
|
62
62
|
"typescript": "^5.9.3",
|
|
63
|
-
"typescript-eslint": "^8.
|
|
64
|
-
"vitest": "^4.1.
|
|
65
|
-
"vue": "^3.5.
|
|
63
|
+
"typescript-eslint": "^8.58.0",
|
|
64
|
+
"vitest": "^4.1.2",
|
|
65
|
+
"vue": "^3.5.31",
|
|
66
66
|
"vuepress": "2.0.0-rc.27",
|
|
67
67
|
"zod": "^4.3.6"
|
|
68
68
|
},
|
|
69
69
|
"scripts": {
|
|
70
|
-
"build": "rm -rf build && NODE_ENV=production inop src/ build -i __tests__ && tsc --declaration --emitDeclarationOnly",
|
|
70
|
+
"build": "rm -rf build && NODE_ENV=production inop src/ build -i __tests__ -i __bench__ && tsc --declaration --emitDeclarationOnly",
|
|
71
71
|
"lint": "eslint src",
|
|
72
|
-
"bench": "overtake
|
|
72
|
+
"bench": "overtake src/__bench__/benchmark.ts -w 1 --progress",
|
|
73
73
|
"test": "NODE_ENV=test vitest run",
|
|
74
74
|
"docs:build": "vuepress build docs",
|
|
75
75
|
"docs:clean-dev": "vuepress dev docs --clean-cache",
|
package/src/index.ts
CHANGED
|
@@ -151,6 +151,13 @@ export const discriminated = <T>(schemas: Schema<T>[], key: string): Discriminat
|
|
|
151
151
|
return new DiscriminatedCtor(schemas, key);
|
|
152
152
|
};
|
|
153
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Creates a custom validation check.
|
|
156
|
+
* Accepts a predicate function or an object with a compile method for inlined checks.
|
|
157
|
+
*
|
|
158
|
+
* @param fnOrOpts - A predicate function `(value) => boolean` or an object with a `compile` method for code-generating checks.
|
|
159
|
+
* @param message - Optional custom error message.
|
|
160
|
+
*/
|
|
154
161
|
export const check = (
|
|
155
162
|
fnOrOpts: ((v: unknown) => boolean) | { compile: (value: string, ctx: CheckContext) => { check: string; message: string } },
|
|
156
163
|
message?: string,
|
|
@@ -167,54 +174,106 @@ export const check = (
|
|
|
167
174
|
return new CheckCtor(fnOrOpts.compile);
|
|
168
175
|
};
|
|
169
176
|
|
|
177
|
+
/**
|
|
178
|
+
* Validates that a numeric value is greater than or equal to `n`.
|
|
179
|
+
*
|
|
180
|
+
* @param n - The minimum allowed value (inclusive).
|
|
181
|
+
* @param message - Optional custom error message.
|
|
182
|
+
*/
|
|
170
183
|
export const min = (n: number, message?: string): CheckShape =>
|
|
171
184
|
new CheckCtor((v) => ({
|
|
172
185
|
check: `${v} < ${n}`,
|
|
173
186
|
message: message ? JSON.stringify(message) : `\`must be >= ${n}, got \${${v}}\``,
|
|
174
187
|
}));
|
|
175
188
|
|
|
189
|
+
/**
|
|
190
|
+
* Validates that a numeric value is less than or equal to `n`.
|
|
191
|
+
*
|
|
192
|
+
* @param n - The maximum allowed value (inclusive).
|
|
193
|
+
* @param message - Optional custom error message.
|
|
194
|
+
*/
|
|
176
195
|
export const max = (n: number, message?: string): CheckShape =>
|
|
177
196
|
new CheckCtor((v) => ({
|
|
178
197
|
check: `${v} > ${n}`,
|
|
179
198
|
message: message ? JSON.stringify(message) : `\`must be <= ${n}, got \${${v}}\``,
|
|
180
199
|
}));
|
|
181
200
|
|
|
201
|
+
/**
|
|
202
|
+
* Validates that a value is an integer.
|
|
203
|
+
*
|
|
204
|
+
* @param message - Optional custom error message.
|
|
205
|
+
*/
|
|
182
206
|
export const integer = (message?: string): CheckShape =>
|
|
183
207
|
new CheckCtor((v) => ({
|
|
184
208
|
check: `!Number.isInteger(${v})`,
|
|
185
209
|
message: message ? JSON.stringify(message) : `\`must be an integer, got \${${v}}\``,
|
|
186
210
|
}));
|
|
187
211
|
|
|
212
|
+
/**
|
|
213
|
+
* Validates that a value's length is greater than or equal to `n`.
|
|
214
|
+
*
|
|
215
|
+
* @param n - The minimum allowed length (inclusive).
|
|
216
|
+
* @param message - Optional custom error message.
|
|
217
|
+
*/
|
|
188
218
|
export const minLength = (n: number, message?: string): CheckShape =>
|
|
189
219
|
new CheckCtor((v) => ({
|
|
190
220
|
check: `${v}.length < ${n}`,
|
|
191
221
|
message: message ? JSON.stringify(message) : `\`length must be >= ${n}, got \${${v}.length}\``,
|
|
192
222
|
}));
|
|
193
223
|
|
|
224
|
+
/**
|
|
225
|
+
* Validates that a value's length is less than or equal to `n`.
|
|
226
|
+
*
|
|
227
|
+
* @param n - The maximum allowed length (inclusive).
|
|
228
|
+
* @param message - Optional custom error message.
|
|
229
|
+
*/
|
|
194
230
|
export const maxLength = (n: number, message?: string): CheckShape =>
|
|
195
231
|
new CheckCtor((v) => ({
|
|
196
232
|
check: `${v}.length > ${n}`,
|
|
197
233
|
message: message ? JSON.stringify(message) : `\`length must be <= ${n}, got \${${v}.length}\``,
|
|
198
234
|
}));
|
|
199
235
|
|
|
236
|
+
/**
|
|
237
|
+
* Validates that a numeric value is strictly greater than `n`.
|
|
238
|
+
*
|
|
239
|
+
* @param n - The exclusive lower bound.
|
|
240
|
+
* @param message - Optional custom error message.
|
|
241
|
+
*/
|
|
200
242
|
export const gt = (n: number, message?: string): CheckShape =>
|
|
201
243
|
new CheckCtor((v) => ({
|
|
202
244
|
check: `${v} <= ${n}`,
|
|
203
245
|
message: message ? JSON.stringify(message) : `\`must be > ${n}, got \${${v}}\``,
|
|
204
246
|
}));
|
|
205
247
|
|
|
248
|
+
/**
|
|
249
|
+
* Validates that a numeric value is strictly less than `n`.
|
|
250
|
+
*
|
|
251
|
+
* @param n - The exclusive upper bound.
|
|
252
|
+
* @param message - Optional custom error message.
|
|
253
|
+
*/
|
|
206
254
|
export const lt = (n: number, message?: string): CheckShape =>
|
|
207
255
|
new CheckCtor((v) => ({
|
|
208
256
|
check: `${v} >= ${n}`,
|
|
209
257
|
message: message ? JSON.stringify(message) : `\`must be < ${n}, got \${${v}}\``,
|
|
210
258
|
}));
|
|
211
259
|
|
|
260
|
+
/**
|
|
261
|
+
* Validates that a numeric value is a multiple of `n`.
|
|
262
|
+
*
|
|
263
|
+
* @param n - The divisor to check against.
|
|
264
|
+
* @param message - Optional custom error message.
|
|
265
|
+
*/
|
|
212
266
|
export const multipleOf = (n: number, message?: string): CheckShape =>
|
|
213
267
|
new CheckCtor((v) => ({
|
|
214
268
|
check: `${v} % ${n} !== 0`,
|
|
215
269
|
message: message ? JSON.stringify(message) : `\`must be a multiple of ${n}, got \${${v}}\``,
|
|
216
270
|
}));
|
|
217
271
|
|
|
272
|
+
/**
|
|
273
|
+
* Validates that an array contains only unique items.
|
|
274
|
+
*
|
|
275
|
+
* @param message - Optional custom error message.
|
|
276
|
+
*/
|
|
218
277
|
export const uniqueItems = (message?: string): CheckShape =>
|
|
219
278
|
new CheckCtor((v) => ({
|
|
220
279
|
check: `new Set(${v}).size !== ${v}.length`,
|
|
@@ -223,6 +282,12 @@ export const uniqueItems = (message?: string): CheckShape =>
|
|
|
223
282
|
|
|
224
283
|
type EnumLike = { [k: string]: string | number; [n: number]: string };
|
|
225
284
|
|
|
285
|
+
/**
|
|
286
|
+
* Validates that a value is one of the allowed values. Accepts an array or an enum-like object.
|
|
287
|
+
*
|
|
288
|
+
* @param values - Array of allowed values or an enum-like object.
|
|
289
|
+
* @param message - Optional custom error message.
|
|
290
|
+
*/
|
|
226
291
|
export const oneOf = <T extends EnumLike>(values: (string | number)[] | T, message?: string): CheckShape => {
|
|
227
292
|
const set = new Set(Array.isArray(values) ? values : Object.values(values));
|
|
228
293
|
return new CheckCtor((v, ctx) => ({
|
|
@@ -266,6 +331,11 @@ const TIME_REGEX = /^(\d*\.?\d*)(ms|s|m|h|d|w)?$/;
|
|
|
266
331
|
*/
|
|
267
332
|
export const asError = <T>(message: string) => new TypeError(message) as unknown as T;
|
|
268
333
|
|
|
334
|
+
/**
|
|
335
|
+
* Type casting utilities for parsing strings into typed values.
|
|
336
|
+
* Useful for environment variables, query parameters, and other string inputs.
|
|
337
|
+
* Returns a TypeError for invalid values, enabling deferred validation with `ascertain()`.
|
|
338
|
+
*/
|
|
269
339
|
export const as = {
|
|
270
340
|
/**
|
|
271
341
|
* Attempts to convert a value to a string.
|
|
@@ -447,6 +517,10 @@ const fnFormat = (fn: (s: string) => boolean, name: string, message?: string): C
|
|
|
447
517
|
message: message ? JSON.stringify(message) : `\`must be a valid ${name}, got \${${v}}\``,
|
|
448
518
|
}));
|
|
449
519
|
|
|
520
|
+
/**
|
|
521
|
+
* String format validators for common patterns (RFC 3339 date-time, email, URI, UUID, etc.).
|
|
522
|
+
* Each method returns a CheckShape that can be composed with `and()` for schema validation.
|
|
523
|
+
*/
|
|
450
524
|
export const format = {
|
|
451
525
|
dateTime: (message?: string): CheckShape => regexFormat(DATETIME_RE, 'date-time', message),
|
|
452
526
|
date: (message?: string): CheckShape => fnFormat(isValidDate, 'date', message),
|
|
@@ -1150,6 +1224,14 @@ interface StandardSchemaFn<T> {
|
|
|
1150
1224
|
'~standard': StandardSchemaV1.Props<T, T>;
|
|
1151
1225
|
}
|
|
1152
1226
|
|
|
1227
|
+
/**
|
|
1228
|
+
* Wraps a schema for Standard Schema v1 compliance.
|
|
1229
|
+
* The returned function throws on invalid data and exposes a `~standard` interface
|
|
1230
|
+
* for interoperability with tRPC, TanStack Form, and other ecosystem tools.
|
|
1231
|
+
*
|
|
1232
|
+
* @param schema - The schema to wrap.
|
|
1233
|
+
* @returns A validator function with a `~standard` property conforming to Standard Schema v1.
|
|
1234
|
+
*/
|
|
1153
1235
|
export const standardSchema = <T>(schema: Schema<T>): StandardSchemaFn<T> => {
|
|
1154
1236
|
const validator = compile(schema);
|
|
1155
1237
|
|