@naturalcycles/nodejs-lib 15.11.0 → 15.12.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,5 +1,6 @@
|
|
|
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 ZodType } from '@naturalcycles/js-lib/zod';
|
|
3
4
|
import type { Ajv } from 'ajv';
|
|
4
5
|
import { AjvValidationError } from './ajvValidationError.js';
|
|
5
6
|
export interface AjvValidationOptions {
|
|
@@ -38,8 +39,6 @@ export interface AjvSchemaCfg {
|
|
|
38
39
|
/**
|
|
39
40
|
* On creation - compiles ajv validation function.
|
|
40
41
|
* Provides convenient methods, error reporting, etc.
|
|
41
|
-
*
|
|
42
|
-
* @experimental
|
|
43
42
|
*/
|
|
44
43
|
export declare class AjvSchema<T = unknown> {
|
|
45
44
|
schema: JsonSchema<T>;
|
|
@@ -58,6 +57,7 @@ export declare class AjvSchema<T = unknown> {
|
|
|
58
57
|
* correctly for some reason.
|
|
59
58
|
*/
|
|
60
59
|
static create<T>(schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
|
|
60
|
+
static createFromZod<T>(zodSchema: ZodType<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
|
|
61
61
|
readonly cfg: AjvSchemaCfg;
|
|
62
62
|
/**
|
|
63
63
|
* It returns the original object just for convenience.
|
|
@@ -2,14 +2,13 @@ import { _isObject, _lazyValue, } from '@naturalcycles/js-lib';
|
|
|
2
2
|
import { JsonSchemaAnyBuilder } from '@naturalcycles/js-lib/json-schema';
|
|
3
3
|
import { _filterNullishValues } from '@naturalcycles/js-lib/object';
|
|
4
4
|
import { _substringBefore } from '@naturalcycles/js-lib/string';
|
|
5
|
+
import { z } from '@naturalcycles/js-lib/zod';
|
|
5
6
|
import { _inspect } from '../../string/inspect.js';
|
|
6
7
|
import { AjvValidationError } from './ajvValidationError.js';
|
|
7
8
|
import { getAjv } from './getAjv.js';
|
|
8
9
|
/**
|
|
9
10
|
* On creation - compiles ajv validation function.
|
|
10
11
|
* Provides convenient methods, error reporting, etc.
|
|
11
|
-
*
|
|
12
|
-
* @experimental
|
|
13
12
|
*/
|
|
14
13
|
export class AjvSchema {
|
|
15
14
|
schema;
|
|
@@ -29,7 +28,7 @@ export class AjvSchema {
|
|
|
29
28
|
/**
|
|
30
29
|
* Shortcut for AjvSchema.create(schema, { lazy: true })
|
|
31
30
|
*/
|
|
32
|
-
static createLazy(schema, cfg
|
|
31
|
+
static createLazy(schema, cfg) {
|
|
33
32
|
return AjvSchema.create(schema, {
|
|
34
33
|
lazy: true,
|
|
35
34
|
...cfg,
|
|
@@ -44,7 +43,7 @@ export class AjvSchema {
|
|
|
44
43
|
* Implementation note: JsonSchemaBuilder goes first in the union type, otherwise TypeScript fails to infer <T> type
|
|
45
44
|
* correctly for some reason.
|
|
46
45
|
*/
|
|
47
|
-
static create(schema, cfg
|
|
46
|
+
static create(schema, cfg) {
|
|
48
47
|
if (schema instanceof AjvSchema)
|
|
49
48
|
return schema;
|
|
50
49
|
if (schema instanceof JsonSchemaAnyBuilder) {
|
|
@@ -52,6 +51,10 @@ export class AjvSchema {
|
|
|
52
51
|
}
|
|
53
52
|
return new AjvSchema(schema, cfg);
|
|
54
53
|
}
|
|
54
|
+
static createFromZod(zodSchema, cfg) {
|
|
55
|
+
const jsonSchema = z.toJSONSchema(zodSchema, { target: 'draft-7' });
|
|
56
|
+
return new AjvSchema(jsonSchema, cfg);
|
|
57
|
+
}
|
|
55
58
|
cfg;
|
|
56
59
|
/**
|
|
57
60
|
* It returns the original object just for convenience.
|
package/package.json
CHANGED
|
@@ -8,6 +8,7 @@ import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib/json-s
|
|
|
8
8
|
import { JsonSchemaAnyBuilder } from '@naturalcycles/js-lib/json-schema'
|
|
9
9
|
import { _filterNullishValues } from '@naturalcycles/js-lib/object'
|
|
10
10
|
import { _substringBefore } from '@naturalcycles/js-lib/string'
|
|
11
|
+
import { z, type ZodType } from '@naturalcycles/js-lib/zod'
|
|
11
12
|
import type { Ajv } from 'ajv'
|
|
12
13
|
import { _inspect } from '../../string/inspect.js'
|
|
13
14
|
import { AjvValidationError } from './ajvValidationError.js'
|
|
@@ -55,8 +56,6 @@ export interface AjvSchemaCfg {
|
|
|
55
56
|
/**
|
|
56
57
|
* On creation - compiles ajv validation function.
|
|
57
58
|
* Provides convenient methods, error reporting, etc.
|
|
58
|
-
*
|
|
59
|
-
* @experimental
|
|
60
59
|
*/
|
|
61
60
|
export class AjvSchema<T = unknown> {
|
|
62
61
|
private constructor(
|
|
@@ -81,7 +80,7 @@ export class AjvSchema<T = unknown> {
|
|
|
81
80
|
*/
|
|
82
81
|
static createLazy<T>(
|
|
83
82
|
schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>,
|
|
84
|
-
cfg
|
|
83
|
+
cfg?: Partial<AjvSchemaCfg>,
|
|
85
84
|
): AjvSchema<T> {
|
|
86
85
|
return AjvSchema.create(schema, {
|
|
87
86
|
lazy: true,
|
|
@@ -100,7 +99,7 @@ export class AjvSchema<T = unknown> {
|
|
|
100
99
|
*/
|
|
101
100
|
static create<T>(
|
|
102
101
|
schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>,
|
|
103
|
-
cfg
|
|
102
|
+
cfg?: Partial<AjvSchemaCfg>,
|
|
104
103
|
): AjvSchema<T> {
|
|
105
104
|
if (schema instanceof AjvSchema) return schema
|
|
106
105
|
if (schema instanceof JsonSchemaAnyBuilder) {
|
|
@@ -109,6 +108,11 @@ export class AjvSchema<T = unknown> {
|
|
|
109
108
|
return new AjvSchema<T>(schema as JsonSchema<T>, cfg)
|
|
110
109
|
}
|
|
111
110
|
|
|
111
|
+
static createFromZod<T>(zodSchema: ZodType<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T> {
|
|
112
|
+
const jsonSchema = z.toJSONSchema(zodSchema, { target: 'draft-7' })
|
|
113
|
+
return new AjvSchema<T>(jsonSchema as JsonSchema<T>, cfg)
|
|
114
|
+
}
|
|
115
|
+
|
|
112
116
|
readonly cfg: AjvSchemaCfg
|
|
113
117
|
|
|
114
118
|
/**
|