@naturalcycles/nodejs-lib 15.56.1 → 15.57.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/ajvSchema.js +1 -0
- package/dist/validation/ajv/getAjv.js +21 -18
- package/dist/validation/ajv/jsonSchemaBuilder.d.ts +2 -0
- package/dist/validation/ajv/jsonSchemaBuilder.js +6 -5
- package/package.json +1 -1
- package/src/validation/ajv/ajvSchema.ts +1 -0
- package/src/validation/ajv/getAjv.ts +27 -18
- package/src/validation/ajv/jsonSchemaBuilder.ts +10 -5
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
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
|
-
import {
|
|
4
|
+
import { Ajv } from 'ajv';
|
|
5
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
|
|
@@ -66,27 +66,30 @@ export function createAjv(opt) {
|
|
|
66
66
|
modifying: true,
|
|
67
67
|
schemaType: 'object',
|
|
68
68
|
errors: true,
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
69
|
+
validate: function validate(transform, data, _schema, ctx) {
|
|
70
|
+
if (!data)
|
|
71
|
+
return true;
|
|
72
|
+
let transformedData = data;
|
|
73
|
+
if (transform.trim) {
|
|
74
|
+
transformedData = transformedData.trim();
|
|
74
75
|
}
|
|
75
|
-
if (
|
|
76
|
-
|
|
76
|
+
if (transform.toLowerCase) {
|
|
77
|
+
transformedData = transformedData.toLocaleLowerCase();
|
|
77
78
|
}
|
|
78
|
-
if (
|
|
79
|
-
|
|
79
|
+
if (transform.toUpperCase) {
|
|
80
|
+
transformedData = transformedData.toLocaleUpperCase();
|
|
80
81
|
}
|
|
81
|
-
if (typeof
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
|
|
82
|
+
if (typeof transform.truncate === 'number' && transform.truncate >= 0) {
|
|
83
|
+
transformedData = transformedData.slice(0, transform.truncate);
|
|
84
|
+
if (transform.trim) {
|
|
85
|
+
transformedData = transformedData.trim();
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
// Explicit check for `undefined` because parentDataProperty can be `0` when it comes to arrays.
|
|
89
|
+
if (ctx?.parentData && typeof ctx.parentDataProperty !== 'undefined') {
|
|
90
|
+
ctx.parentData[ctx.parentDataProperty] = transformedData;
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
90
93
|
},
|
|
91
94
|
});
|
|
92
95
|
ajv.addKeyword({
|
|
@@ -366,7 +369,7 @@ export function createAjv(opt) {
|
|
|
366
369
|
if (!optionalValues.includes(data))
|
|
367
370
|
return true;
|
|
368
371
|
if (ctx?.parentData && ctx.parentDataProperty) {
|
|
369
|
-
ctx.parentData[ctx.parentDataProperty]
|
|
372
|
+
delete ctx.parentData[ctx.parentDataProperty];
|
|
370
373
|
}
|
|
371
374
|
return true;
|
|
372
375
|
},
|
|
@@ -86,6 +86,7 @@ export declare class JsonSchemaStringBuilder<IN extends string | undefined = str
|
|
|
86
86
|
pattern(pattern: string, opt?: JsonBuilderRuleOpt): this;
|
|
87
87
|
minLength(minLength: number): this;
|
|
88
88
|
maxLength(maxLength: number): this;
|
|
89
|
+
length(exactLength: number): this;
|
|
89
90
|
length(minLength: number, maxLength: number): this;
|
|
90
91
|
email(opt?: Partial<JsonSchemaStringEmailOptions>): this;
|
|
91
92
|
trim(): this;
|
|
@@ -234,6 +235,7 @@ export declare class JsonSchemaArrayBuilder<IN, OUT, Opt> extends JsonSchemaAnyB
|
|
|
234
235
|
constructor(itemsSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>);
|
|
235
236
|
minLength(minItems: number): this;
|
|
236
237
|
maxLength(maxItems: number): this;
|
|
238
|
+
length(exactLength: number): this;
|
|
237
239
|
length(minItems: number, maxItems: number): this;
|
|
238
240
|
exactLength(length: number): this;
|
|
239
241
|
unique(): this;
|
|
@@ -248,9 +248,9 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
|
|
|
248
248
|
_objectAssign(this.schema, { maxLength });
|
|
249
249
|
return this;
|
|
250
250
|
}
|
|
251
|
-
length(
|
|
252
|
-
|
|
253
|
-
return this;
|
|
251
|
+
length(minLengthOrExactLength, maxLength) {
|
|
252
|
+
const maxLengthActual = maxLength ?? minLengthOrExactLength;
|
|
253
|
+
return this.minLength(minLengthOrExactLength).maxLength(maxLengthActual);
|
|
254
254
|
}
|
|
255
255
|
email(opt) {
|
|
256
256
|
const defaultOptions = { checkTLD: true };
|
|
@@ -649,8 +649,9 @@ export class JsonSchemaArrayBuilder extends JsonSchemaAnyBuilder {
|
|
|
649
649
|
_objectAssign(this.schema, { maxItems });
|
|
650
650
|
return this;
|
|
651
651
|
}
|
|
652
|
-
length(
|
|
653
|
-
|
|
652
|
+
length(minItemsOrExact, maxItems) {
|
|
653
|
+
const maxItemsActual = maxItems ?? minItemsOrExact;
|
|
654
|
+
return this.minLength(minItemsOrExact).maxLength(maxItemsActual);
|
|
654
655
|
}
|
|
655
656
|
exactLength(length) {
|
|
656
657
|
return this.minLength(length).maxLength(length);
|
package/package.json
CHANGED
|
@@ -73,6 +73,7 @@ export class AjvSchema<IN = unknown, OUT = IN> {
|
|
|
73
73
|
let jsonSchema: JsonSchema<IN, OUT>
|
|
74
74
|
|
|
75
75
|
if (AjvSchema.isJsonSchemaBuilder(schema)) {
|
|
76
|
+
// oxlint-disable typescript-eslint(no-unnecessary-type-assertion)
|
|
76
77
|
jsonSchema = (schema as JsonSchemaTerminal<IN, OUT, any>).build()
|
|
77
78
|
AjvSchema.requireValidJsonSchema(jsonSchema)
|
|
78
79
|
} else {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
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
|
-
import {
|
|
4
|
+
import { Ajv, type Options, type ValidateFunction } from 'ajv'
|
|
5
5
|
import { validTLDs } from '../tlds.js'
|
|
6
6
|
import type { JsonSchemaIsoDateOptions, JsonSchemaStringEmailOptions } from './jsonSchemaBuilder.js'
|
|
7
7
|
|
|
@@ -77,33 +77,42 @@ export function createAjv(opt?: Options): Ajv {
|
|
|
77
77
|
modifying: true,
|
|
78
78
|
schemaType: 'object',
|
|
79
79
|
errors: true,
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
validate: function validate(
|
|
81
|
+
transform: { trim?: true; toLowerCase?: true; toUpperCase?: true; truncate?: number },
|
|
82
|
+
data: string,
|
|
83
|
+
_schema,
|
|
84
|
+
ctx,
|
|
85
|
+
) {
|
|
86
|
+
if (!data) return true
|
|
83
87
|
|
|
84
|
-
|
|
85
|
-
|
|
88
|
+
let transformedData = data
|
|
89
|
+
|
|
90
|
+
if (transform.trim) {
|
|
91
|
+
transformedData = transformedData.trim()
|
|
86
92
|
}
|
|
87
93
|
|
|
88
|
-
if (
|
|
89
|
-
|
|
94
|
+
if (transform.toLowerCase) {
|
|
95
|
+
transformedData = transformedData.toLocaleLowerCase()
|
|
90
96
|
}
|
|
91
97
|
|
|
92
|
-
if (
|
|
93
|
-
|
|
98
|
+
if (transform.toUpperCase) {
|
|
99
|
+
transformedData = transformedData.toLocaleUpperCase()
|
|
94
100
|
}
|
|
95
101
|
|
|
96
|
-
if (typeof
|
|
97
|
-
|
|
102
|
+
if (typeof transform.truncate === 'number' && transform.truncate >= 0) {
|
|
103
|
+
transformedData = transformedData.slice(0, transform.truncate)
|
|
98
104
|
|
|
99
|
-
if (
|
|
100
|
-
|
|
105
|
+
if (transform.trim) {
|
|
106
|
+
transformedData = transformedData.trim()
|
|
101
107
|
}
|
|
102
108
|
}
|
|
103
109
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
110
|
+
// Explicit check for `undefined` because parentDataProperty can be `0` when it comes to arrays.
|
|
111
|
+
if (ctx?.parentData && typeof ctx.parentDataProperty !== 'undefined') {
|
|
112
|
+
ctx.parentData[ctx.parentDataProperty] = transformedData
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return true
|
|
107
116
|
},
|
|
108
117
|
})
|
|
109
118
|
|
|
@@ -413,7 +422,7 @@ export function createAjv(opt?: Options): Ajv {
|
|
|
413
422
|
if (!optionalValues.includes(data)) return true
|
|
414
423
|
|
|
415
424
|
if (ctx?.parentData && ctx.parentDataProperty) {
|
|
416
|
-
ctx.parentData[ctx.parentDataProperty]
|
|
425
|
+
delete ctx.parentData[ctx.parentDataProperty]
|
|
417
426
|
}
|
|
418
427
|
|
|
419
428
|
return true
|
|
@@ -364,9 +364,11 @@ export class JsonSchemaStringBuilder<
|
|
|
364
364
|
return this
|
|
365
365
|
}
|
|
366
366
|
|
|
367
|
-
length(
|
|
368
|
-
|
|
369
|
-
|
|
367
|
+
length(exactLength: number): this
|
|
368
|
+
length(minLength: number, maxLength: number): this
|
|
369
|
+
length(minLengthOrExactLength: number, maxLength?: number): this {
|
|
370
|
+
const maxLengthActual = maxLength ?? minLengthOrExactLength
|
|
371
|
+
return this.minLength(minLengthOrExactLength).maxLength(maxLengthActual)
|
|
370
372
|
}
|
|
371
373
|
|
|
372
374
|
email(opt?: Partial<JsonSchemaStringEmailOptions>): this {
|
|
@@ -952,8 +954,11 @@ export class JsonSchemaArrayBuilder<IN, OUT, Opt> extends JsonSchemaAnyBuilder<I
|
|
|
952
954
|
return this
|
|
953
955
|
}
|
|
954
956
|
|
|
955
|
-
length(
|
|
956
|
-
|
|
957
|
+
length(exactLength: number): this
|
|
958
|
+
length(minItems: number, maxItems: number): this
|
|
959
|
+
length(minItemsOrExact: number, maxItems?: number): this {
|
|
960
|
+
const maxItemsActual = maxItems ?? minItemsOrExact
|
|
961
|
+
return this.minLength(minItemsOrExact).maxLength(maxItemsActual)
|
|
957
962
|
}
|
|
958
963
|
|
|
959
964
|
exactLength(length: number): this {
|