@naturalcycles/nodejs-lib 15.44.0 → 15.46.0
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/validation/ajv/getAjv.js +1 -1
- package/dist/validation/ajv/jsonSchemaBuilder.d.ts +25 -10
- package/dist/validation/ajv/jsonSchemaBuilder.js +36 -13
- package/dist/validation/timezones.d.ts +9 -0
- package/dist/validation/timezones.js +612 -0
- package/package.json +1 -1
- package/src/validation/ajv/getAjv.ts +1 -1
- package/src/validation/ajv/jsonSchemaBuilder.ts +53 -14
- package/src/validation/timezones.ts +616 -0
- /package/dist/validation/{ajv/tlds.d.ts → tlds.d.ts} +0 -0
- /package/dist/validation/{ajv/tlds.js → tlds.js} +0 -0
- /package/src/validation/{ajv/tlds.ts → tlds.ts} +0 -0
|
@@ -2,7 +2,7 @@ import { _lazyValue } from '@naturalcycles/js-lib';
|
|
|
2
2
|
import { Set2 } from '@naturalcycles/js-lib/object';
|
|
3
3
|
import { _substringAfterLast } from '@naturalcycles/js-lib/string';
|
|
4
4
|
import { _, Ajv } from 'ajv';
|
|
5
|
-
import { validTLDs } from '
|
|
5
|
+
import { validTLDs } from '../tlds.js';
|
|
6
6
|
/* eslint-disable @typescript-eslint/prefer-string-starts-ends-with */
|
|
7
7
|
// oxlint-disable unicorn/prefer-code-point
|
|
8
8
|
const AJV_OPTIONS = {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Set2 } from '@naturalcycles/js-lib/object';
|
|
2
|
-
import { type AnyObject, type IANATimezone, type IsoDate, type IsoDateTime, type NumberEnum, type StringEnum, type StringMap, type UnixTimestamp, type UnixTimestampMillis } from '@naturalcycles/js-lib/types';
|
|
2
|
+
import { type AnyObject, type IANATimezone, type Inclusiveness, type IsoDate, type IsoDateTime, type NumberEnum, type StringEnum, type StringMap, type UnixTimestamp, type UnixTimestampMillis } from '@naturalcycles/js-lib/types';
|
|
3
3
|
export declare const j: {
|
|
4
4
|
string(): JsonSchemaStringBuilder<string, string, false>;
|
|
5
5
|
number(): JsonSchemaNumberBuilder<number, number, false>;
|
|
@@ -66,8 +66,8 @@ export declare class JsonSchemaStringBuilder<IN extends string = string, OUT = I
|
|
|
66
66
|
constructor();
|
|
67
67
|
regex(pattern: RegExp, opt?: JsonBuilderRuleOpt): this;
|
|
68
68
|
pattern(pattern: string, opt?: JsonBuilderRuleOpt): this;
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
minLength(minLength: number): this;
|
|
70
|
+
maxLength(maxLength: number): this;
|
|
71
71
|
length(minLength: number, maxLength: number): this;
|
|
72
72
|
email(opt?: Partial<JsonSchemaStringEmailOptions>): this;
|
|
73
73
|
trim(): this;
|
|
@@ -111,10 +111,12 @@ export declare class JsonSchemaNumberBuilder<IN extends number = number, OUT = I
|
|
|
111
111
|
exclusiveMin(exclusiveMinimum: number): this;
|
|
112
112
|
max(maximum: number): this;
|
|
113
113
|
exclusiveMax(exclusiveMaximum: number): this;
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
114
|
+
lessThan(value: number): this;
|
|
115
|
+
lessThanOrEqual(value: number): this;
|
|
116
|
+
moreThan(value: number): this;
|
|
117
|
+
moreThanOrEqual(value: number): this;
|
|
118
|
+
equal(value: number): this;
|
|
119
|
+
range(minimum: number, maximum: number, incl: Inclusiveness): this;
|
|
118
120
|
int32(): this;
|
|
119
121
|
int64(): this;
|
|
120
122
|
float(): this;
|
|
@@ -163,7 +165,7 @@ export declare class JsonSchemaObjectInferringBuilder<PROPS extends Record<strin
|
|
|
163
165
|
/**
|
|
164
166
|
* Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
|
|
165
167
|
*/
|
|
166
|
-
dbEntity(): JsonSchemaObjectInferringBuilder<{ [K in keyof PROPS | "
|
|
168
|
+
dbEntity(): JsonSchemaObjectInferringBuilder<{ [K in "id" | keyof PROPS | "created" | "updated"]: K extends "id" | "created" | "updated" ? {
|
|
167
169
|
id: JsonSchemaStringBuilder<string, string, false>;
|
|
168
170
|
created: JsonSchemaNumberBuilder<UnixTimestamp, UnixTimestamp, false>;
|
|
169
171
|
updated: JsonSchemaNumberBuilder<UnixTimestamp, UnixTimestamp, false>;
|
|
@@ -171,8 +173,10 @@ export declare class JsonSchemaObjectInferringBuilder<PROPS extends Record<strin
|
|
|
171
173
|
}
|
|
172
174
|
export declare class JsonSchemaArrayBuilder<IN, OUT, Opt> extends JsonSchemaAnyBuilder<IN[], OUT[], Opt> {
|
|
173
175
|
constructor(itemsSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>);
|
|
174
|
-
|
|
175
|
-
|
|
176
|
+
minLength(minItems: number): this;
|
|
177
|
+
maxLength(maxItems: number): this;
|
|
178
|
+
length(minItems: number, maxItems: number): this;
|
|
179
|
+
exactLength(length: number): this;
|
|
176
180
|
unique(uniqueItems: number): this;
|
|
177
181
|
}
|
|
178
182
|
export declare class JsonSchemaSet2Builder<IN, OUT, Opt> extends JsonSchemaAnyBuilder<Iterable<IN>, Set2<OUT>, Opt> {
|
|
@@ -264,6 +268,17 @@ type BuilderInUnion<B extends readonly JsonSchemaAnyBuilder<any, any, any>[]> =
|
|
|
264
268
|
[K in keyof B]: B[K] extends JsonSchemaAnyBuilder<infer I, any, any> ? I : never;
|
|
265
269
|
}[number];
|
|
266
270
|
interface JsonBuilderRuleOpt {
|
|
271
|
+
/**
|
|
272
|
+
* Text of error message to return when the validation fails for the given rule:
|
|
273
|
+
*
|
|
274
|
+
* `{ msg: "is not a valid Oompa-loompa" } => "Object.property is not a valid Oompa-loompa"`
|
|
275
|
+
*/
|
|
267
276
|
msg?: string;
|
|
277
|
+
/**
|
|
278
|
+
* A friendly name for what we are validating, that will be used in error messages:
|
|
279
|
+
*
|
|
280
|
+
* `{ name: "Oompa-loompa" } => "Object.property is not a valid Oompa-loompa"`
|
|
281
|
+
*/
|
|
282
|
+
name?: string;
|
|
268
283
|
}
|
|
269
284
|
export {};
|
|
@@ -5,6 +5,7 @@ import { _uniq } from '@naturalcycles/js-lib/array';
|
|
|
5
5
|
import { _assert } from '@naturalcycles/js-lib/error';
|
|
6
6
|
import { _deepCopy, _sortObject } from '@naturalcycles/js-lib/object';
|
|
7
7
|
import { JWT_REGEX, } from '@naturalcycles/js-lib/types';
|
|
8
|
+
import { TIMEZONES } from '../timezones.js';
|
|
8
9
|
import { JSON_SCHEMA_ORDER, mergeJsonSchemaObjects } from './jsonSchemaBuilder.util.js';
|
|
9
10
|
export const j = {
|
|
10
11
|
string() {
|
|
@@ -177,16 +178,18 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
|
|
|
177
178
|
return this.pattern(pattern.source, opt);
|
|
178
179
|
}
|
|
179
180
|
pattern(pattern, opt) {
|
|
181
|
+
if (opt?.name)
|
|
182
|
+
this.setErrorMessage('pattern', `is not a valid ${opt.name}`);
|
|
180
183
|
if (opt?.msg)
|
|
181
184
|
this.setErrorMessage('pattern', opt.msg);
|
|
182
185
|
Object.assign(this.schema, { pattern });
|
|
183
186
|
return this;
|
|
184
187
|
}
|
|
185
|
-
|
|
188
|
+
minLength(minLength) {
|
|
186
189
|
Object.assign(this.schema, { minLength });
|
|
187
190
|
return this;
|
|
188
191
|
}
|
|
189
|
-
|
|
192
|
+
maxLength(maxLength) {
|
|
190
193
|
Object.assign(this.schema, { maxLength });
|
|
191
194
|
return this;
|
|
192
195
|
}
|
|
@@ -283,9 +286,7 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
|
|
|
283
286
|
*/
|
|
284
287
|
ianaTimezone() {
|
|
285
288
|
// UTC is added to assist unit-testing, which uses UTC by default (not technically a valid Iana timezone identifier)
|
|
286
|
-
return j
|
|
287
|
-
.enum([...Intl.supportedValuesOf('timeZone'), 'UTC'], { msg: 'is an invalid IANA timezone' })
|
|
288
|
-
.branded();
|
|
289
|
+
return j.enum(TIMEZONES, { msg: 'is an invalid IANA timezone' }).branded();
|
|
289
290
|
}
|
|
290
291
|
}
|
|
291
292
|
export class JsonSchemaNumberBuilder extends JsonSchemaAnyBuilder {
|
|
@@ -321,12 +322,26 @@ export class JsonSchemaNumberBuilder extends JsonSchemaAnyBuilder {
|
|
|
321
322
|
Object.assign(this.schema, { exclusiveMaximum });
|
|
322
323
|
return this;
|
|
323
324
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
325
|
+
lessThan(value) {
|
|
326
|
+
return this.exclusiveMax(value);
|
|
327
|
+
}
|
|
328
|
+
lessThanOrEqual(value) {
|
|
329
|
+
return this.max(value);
|
|
330
|
+
}
|
|
331
|
+
moreThan(value) {
|
|
332
|
+
return this.exclusiveMin(value);
|
|
333
|
+
}
|
|
334
|
+
moreThanOrEqual(value) {
|
|
335
|
+
return this.min(value);
|
|
336
|
+
}
|
|
337
|
+
equal(value) {
|
|
338
|
+
return this.min(value).max(value);
|
|
339
|
+
}
|
|
340
|
+
range(minimum, maximum, incl) {
|
|
341
|
+
if (incl === '[)') {
|
|
342
|
+
return this.moreThanOrEqual(minimum).lessThan(maximum);
|
|
343
|
+
}
|
|
344
|
+
return this.moreThanOrEqual(minimum).lessThanOrEqual(maximum);
|
|
330
345
|
}
|
|
331
346
|
int32() {
|
|
332
347
|
const MIN_INT32 = -(2 ** 31);
|
|
@@ -495,14 +510,20 @@ export class JsonSchemaArrayBuilder extends JsonSchemaAnyBuilder {
|
|
|
495
510
|
items: itemsSchema.build(),
|
|
496
511
|
});
|
|
497
512
|
}
|
|
498
|
-
|
|
513
|
+
minLength(minItems) {
|
|
499
514
|
Object.assign(this.schema, { minItems });
|
|
500
515
|
return this;
|
|
501
516
|
}
|
|
502
|
-
|
|
517
|
+
maxLength(maxItems) {
|
|
503
518
|
Object.assign(this.schema, { maxItems });
|
|
504
519
|
return this;
|
|
505
520
|
}
|
|
521
|
+
length(minItems, maxItems) {
|
|
522
|
+
return this.minLength(minItems).maxLength(maxItems);
|
|
523
|
+
}
|
|
524
|
+
exactLength(length) {
|
|
525
|
+
return this.minLength(length).maxLength(length);
|
|
526
|
+
}
|
|
506
527
|
unique(uniqueItems) {
|
|
507
528
|
Object.assign(this.schema, { uniqueItems });
|
|
508
529
|
return this;
|
|
@@ -534,6 +555,8 @@ export class JsonSchemaBufferBuilder extends JsonSchemaAnyBuilder {
|
|
|
534
555
|
export class JsonSchemaEnumBuilder extends JsonSchemaAnyBuilder {
|
|
535
556
|
constructor(enumValues, opt) {
|
|
536
557
|
super({ enum: enumValues });
|
|
558
|
+
if (opt?.name)
|
|
559
|
+
this.setErrorMessage('pattern', `is not a valid ${opt.name}`);
|
|
537
560
|
if (opt?.msg)
|
|
538
561
|
this.setErrorMessage('enum', opt.msg);
|
|
539
562
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A complicated merge of timezones from the underlying Javascript engine
|
|
3
|
+
* and a list exported from Wikipedia.
|
|
4
|
+
*
|
|
5
|
+
* Why? The `Int.supportedValuesOf('timeZone')` we use provide only
|
|
6
|
+
* canonical/standardized timezone values like `Europe/Stockholm`
|
|
7
|
+
* but does not list accepted aliases like `ETC/Gmt`, `CET` or `Canada/Atlantic`.
|
|
8
|
+
*/
|
|
9
|
+
export declare const TIMEZONES: any;
|