@naturalcycles/nodejs-lib 15.34.0 → 15.35.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.
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { type ValidationFunction, type ValidationFunctionResult } from '@naturalcycles/js-lib';
|
|
2
2
|
import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib/json-schema';
|
|
3
|
+
import type { AnyObject } from '@naturalcycles/js-lib/types';
|
|
3
4
|
import { ZodType } from '@naturalcycles/js-lib/zod';
|
|
4
5
|
import type { Ajv } from 'ajv';
|
|
5
6
|
import { AjvValidationError } from './ajvValidationError.js';
|
|
7
|
+
export type SchemaHandledByAjv<T> = JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T> | ZodType<T>;
|
|
6
8
|
export interface AjvValidationOptions {
|
|
7
9
|
/**
|
|
8
10
|
* Defaults to true,
|
|
@@ -63,11 +65,15 @@ export declare class AjvSchema<T = unknown> {
|
|
|
63
65
|
* Implementation note: JsonSchemaBuilder goes first in the union type, otherwise TypeScript fails to infer <T> type
|
|
64
66
|
* correctly for some reason.
|
|
65
67
|
*/
|
|
66
|
-
static create<T>(schema:
|
|
68
|
+
static create<T>(schema: SchemaHandledByAjv<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
|
|
67
69
|
/**
|
|
68
|
-
* @
|
|
70
|
+
* @deprecated
|
|
71
|
+
*
|
|
72
|
+
* Use `AjvSchema.create`
|
|
69
73
|
*/
|
|
70
74
|
static createFromZod<T>(zodSchema: ZodType<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
|
|
75
|
+
static isJsonSchemaBuilder<T>(schema: unknown): schema is JsonSchemaBuilder<T>;
|
|
76
|
+
static isZodSchema<T>(schema: unknown): schema is ZodType<T>;
|
|
71
77
|
readonly cfg: AjvSchemaCfg;
|
|
72
78
|
/**
|
|
73
79
|
* It returns the original object just for convenience.
|
|
@@ -81,12 +87,12 @@ export declare class AjvSchema<T = unknown> {
|
|
|
81
87
|
isValid(input: T, opt?: AjvValidationOptions): boolean;
|
|
82
88
|
getValidationResult(input: T, opt?: AjvValidationOptions): ValidationFunctionResult<T, AjvValidationError>;
|
|
83
89
|
getValidationFunction(): ValidationFunction<T, AjvValidationError>;
|
|
84
|
-
static
|
|
85
|
-
static
|
|
86
|
-
static
|
|
90
|
+
static isSchemaWithCachedAjvSchema<Base, T>(schema: Base): schema is WithCachedAjvSchema<Base, T>;
|
|
91
|
+
static cacheAjvSchema<Base extends AnyObject, T>(schema: Base, ajvSchema: AjvSchema<T>): WithCachedAjvSchema<Base, T>;
|
|
92
|
+
static requireCachedAjvSchema<Base, T>(schema: WithCachedAjvSchema<Base, T>): AjvSchema<T>;
|
|
87
93
|
private getAJVValidateFunction;
|
|
88
94
|
}
|
|
89
95
|
export declare const HIDDEN_AJV_SCHEMA: unique symbol;
|
|
90
|
-
export
|
|
96
|
+
export type WithCachedAjvSchema<Base, T> = Base & {
|
|
91
97
|
[HIDDEN_AJV_SCHEMA]: AjvSchema<T>;
|
|
92
|
-
}
|
|
98
|
+
};
|
|
@@ -46,24 +46,36 @@ export class AjvSchema {
|
|
|
46
46
|
static create(schema, cfg) {
|
|
47
47
|
if (schema instanceof AjvSchema)
|
|
48
48
|
return schema;
|
|
49
|
-
if (schema
|
|
50
|
-
return
|
|
49
|
+
if (AjvSchema.isSchemaWithCachedAjvSchema(schema)) {
|
|
50
|
+
return AjvSchema.requireCachedAjvSchema(schema);
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
let jsonSchema;
|
|
53
|
+
if (AjvSchema.isJsonSchemaBuilder(schema)) {
|
|
54
|
+
jsonSchema = schema.build();
|
|
55
|
+
}
|
|
56
|
+
else if (AjvSchema.isZodSchema(schema)) {
|
|
57
|
+
jsonSchema = z.toJSONSchema(schema, { target: 'draft-7' });
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
jsonSchema = schema;
|
|
61
|
+
}
|
|
62
|
+
const ajvSchema = new AjvSchema(jsonSchema, cfg);
|
|
63
|
+
AjvSchema.cacheAjvSchema(schema, ajvSchema);
|
|
64
|
+
return ajvSchema;
|
|
55
65
|
}
|
|
56
66
|
/**
|
|
57
|
-
* @
|
|
67
|
+
* @deprecated
|
|
68
|
+
*
|
|
69
|
+
* Use `AjvSchema.create`
|
|
58
70
|
*/
|
|
59
71
|
static createFromZod(zodSchema, cfg) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return
|
|
72
|
+
return AjvSchema.create(zodSchema, cfg);
|
|
73
|
+
}
|
|
74
|
+
static isJsonSchemaBuilder(schema) {
|
|
75
|
+
return schema instanceof JsonSchemaAnyBuilder;
|
|
76
|
+
}
|
|
77
|
+
static isZodSchema(schema) {
|
|
78
|
+
return schema instanceof ZodType;
|
|
67
79
|
}
|
|
68
80
|
cfg;
|
|
69
81
|
/**
|
|
@@ -121,14 +133,14 @@ export class AjvSchema {
|
|
|
121
133
|
});
|
|
122
134
|
};
|
|
123
135
|
}
|
|
124
|
-
static
|
|
125
|
-
return !!schema[HIDDEN_AJV_SCHEMA];
|
|
136
|
+
static isSchemaWithCachedAjvSchema(schema) {
|
|
137
|
+
return !!schema?.[HIDDEN_AJV_SCHEMA];
|
|
126
138
|
}
|
|
127
|
-
static
|
|
128
|
-
return Object.assign(
|
|
139
|
+
static cacheAjvSchema(schema, ajvSchema) {
|
|
140
|
+
return Object.assign(schema, { [HIDDEN_AJV_SCHEMA]: ajvSchema });
|
|
129
141
|
}
|
|
130
|
-
static
|
|
131
|
-
return
|
|
142
|
+
static requireCachedAjvSchema(schema) {
|
|
143
|
+
return schema[HIDDEN_AJV_SCHEMA];
|
|
132
144
|
}
|
|
133
145
|
getAJVValidateFunction = _lazyValue(() => this.cfg.ajv.compile(this.schema));
|
|
134
146
|
}
|
package/package.json
CHANGED
|
@@ -8,12 +8,15 @@ import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib/json-s
|
|
|
8
8
|
import { JsonSchemaAnyBuilder } from '@naturalcycles/js-lib/json-schema'
|
|
9
9
|
import { _deepCopy, _filterNullishValues } from '@naturalcycles/js-lib/object'
|
|
10
10
|
import { _substringBefore } from '@naturalcycles/js-lib/string'
|
|
11
|
+
import type { AnyObject } from '@naturalcycles/js-lib/types'
|
|
11
12
|
import { z, ZodType } from '@naturalcycles/js-lib/zod'
|
|
12
13
|
import type { Ajv } from 'ajv'
|
|
13
14
|
import { _inspect } from '../../string/inspect.js'
|
|
14
15
|
import { AjvValidationError } from './ajvValidationError.js'
|
|
15
16
|
import { getAjv } from './getAjv.js'
|
|
16
17
|
|
|
18
|
+
export type SchemaHandledByAjv<T> = JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T> | ZodType<T>
|
|
19
|
+
|
|
17
20
|
export interface AjvValidationOptions {
|
|
18
21
|
/**
|
|
19
22
|
* Defaults to true,
|
|
@@ -104,32 +107,44 @@ export class AjvSchema<T = unknown> {
|
|
|
104
107
|
* Implementation note: JsonSchemaBuilder goes first in the union type, otherwise TypeScript fails to infer <T> type
|
|
105
108
|
* correctly for some reason.
|
|
106
109
|
*/
|
|
107
|
-
static create<T>(
|
|
108
|
-
schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T> | ZodType<T>,
|
|
109
|
-
cfg?: Partial<AjvSchemaCfg>,
|
|
110
|
-
): AjvSchema<T> {
|
|
110
|
+
static create<T>(schema: SchemaHandledByAjv<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T> {
|
|
111
111
|
if (schema instanceof AjvSchema) return schema
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
|
|
113
|
+
if (AjvSchema.isSchemaWithCachedAjvSchema<typeof schema, T>(schema)) {
|
|
114
|
+
return AjvSchema.requireCachedAjvSchema<typeof schema, T>(schema)
|
|
114
115
|
}
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
|
|
117
|
+
let jsonSchema: JsonSchema<T>
|
|
118
|
+
|
|
119
|
+
if (AjvSchema.isJsonSchemaBuilder(schema)) {
|
|
120
|
+
jsonSchema = schema.build()
|
|
121
|
+
} else if (AjvSchema.isZodSchema(schema)) {
|
|
122
|
+
jsonSchema = z.toJSONSchema(schema, { target: 'draft-7' }) as JsonSchema<T>
|
|
123
|
+
} else {
|
|
124
|
+
jsonSchema = schema
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const ajvSchema = new AjvSchema<T>(jsonSchema, cfg)
|
|
128
|
+
AjvSchema.cacheAjvSchema(schema, ajvSchema)
|
|
129
|
+
|
|
130
|
+
return ajvSchema
|
|
117
131
|
}
|
|
118
132
|
|
|
119
133
|
/**
|
|
120
|
-
* @
|
|
134
|
+
* @deprecated
|
|
135
|
+
*
|
|
136
|
+
* Use `AjvSchema.create`
|
|
121
137
|
*/
|
|
122
138
|
static createFromZod<T>(zodSchema: ZodType<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T> {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const jsonSchema = z.toJSONSchema(zodSchema, { target: 'draft-7' })
|
|
128
|
-
const ajvSchema = new AjvSchema<T>(jsonSchema as JsonSchema<T>, cfg)
|
|
139
|
+
return AjvSchema.create(zodSchema, cfg)
|
|
140
|
+
}
|
|
129
141
|
|
|
130
|
-
|
|
142
|
+
static isJsonSchemaBuilder<T>(schema: unknown): schema is JsonSchemaBuilder<T> {
|
|
143
|
+
return schema instanceof JsonSchemaAnyBuilder
|
|
144
|
+
}
|
|
131
145
|
|
|
132
|
-
|
|
146
|
+
static isZodSchema<T>(schema: unknown): schema is ZodType<T> {
|
|
147
|
+
return schema instanceof ZodType
|
|
133
148
|
}
|
|
134
149
|
|
|
135
150
|
readonly cfg: AjvSchemaCfg
|
|
@@ -208,19 +223,21 @@ export class AjvSchema<T = unknown> {
|
|
|
208
223
|
}
|
|
209
224
|
}
|
|
210
225
|
|
|
211
|
-
static
|
|
212
|
-
|
|
226
|
+
static isSchemaWithCachedAjvSchema<Base, T>(
|
|
227
|
+
schema: Base,
|
|
228
|
+
): schema is WithCachedAjvSchema<Base, T> {
|
|
229
|
+
return !!(schema as any)?.[HIDDEN_AJV_SCHEMA]
|
|
213
230
|
}
|
|
214
231
|
|
|
215
|
-
static
|
|
216
|
-
|
|
232
|
+
static cacheAjvSchema<Base extends AnyObject, T>(
|
|
233
|
+
schema: Base,
|
|
217
234
|
ajvSchema: AjvSchema<T>,
|
|
218
|
-
):
|
|
219
|
-
return Object.assign(
|
|
235
|
+
): WithCachedAjvSchema<Base, T> {
|
|
236
|
+
return Object.assign(schema, { [HIDDEN_AJV_SCHEMA]: ajvSchema })
|
|
220
237
|
}
|
|
221
238
|
|
|
222
|
-
static
|
|
223
|
-
return
|
|
239
|
+
static requireCachedAjvSchema<Base, T>(schema: WithCachedAjvSchema<Base, T>): AjvSchema<T> {
|
|
240
|
+
return schema[HIDDEN_AJV_SCHEMA]
|
|
224
241
|
}
|
|
225
242
|
|
|
226
243
|
private getAJVValidateFunction = _lazyValue(() => this.cfg.ajv.compile<T>(this.schema))
|
|
@@ -230,6 +247,6 @@ const separator = '\n'
|
|
|
230
247
|
|
|
231
248
|
export const HIDDEN_AJV_SCHEMA = Symbol('HIDDEN_AJV_SCHEMA')
|
|
232
249
|
|
|
233
|
-
export
|
|
250
|
+
export type WithCachedAjvSchema<Base, T> = Base & {
|
|
234
251
|
[HIDDEN_AJV_SCHEMA]: AjvSchema<T>
|
|
235
252
|
}
|