@naturalcycles/nodejs-lib 15.11.0 → 15.13.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.
@@ -1,15 +1,14 @@
1
1
  import { _isObject, _lazyValue, } from '@naturalcycles/js-lib';
2
2
  import { JsonSchemaAnyBuilder } from '@naturalcycles/js-lib/json-schema';
3
- import { _filterNullishValues } from '@naturalcycles/js-lib/object';
3
+ import { _deepCopy, _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.
@@ -73,7 +76,7 @@ export class AjvSchema {
73
76
  }
74
77
  getValidationResult(input, opt = {}) {
75
78
  const fn = this.getAJVValidateFunction();
76
- const item = opt.mutateInput || typeof input !== 'object' ? input : structuredClone(input);
79
+ const item = opt.mutateInput || typeof input !== 'object' ? input : _deepCopy(input);
77
80
  const valid = fn(item); // mutates item
78
81
  if (valid)
79
82
  return [null, item];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.11.0",
4
+ "version": "15.13.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -6,8 +6,9 @@ import {
6
6
  } from '@naturalcycles/js-lib'
7
7
  import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib/json-schema'
8
8
  import { JsonSchemaAnyBuilder } from '@naturalcycles/js-lib/json-schema'
9
- import { _filterNullishValues } from '@naturalcycles/js-lib/object'
9
+ import { _deepCopy, _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: Partial<AjvSchemaCfg> = {},
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: Partial<AjvSchemaCfg> = {},
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
  /**
@@ -136,7 +140,7 @@ export class AjvSchema<T = unknown> {
136
140
  ): ValidationFunctionResult<T, AjvValidationError> {
137
141
  const fn = this.getAJVValidateFunction()
138
142
 
139
- const item = opt.mutateInput || typeof input !== 'object' ? input : structuredClone(input)
143
+ const item = opt.mutateInput || typeof input !== 'object' ? input : _deepCopy(input)
140
144
 
141
145
  const valid = fn(item) // mutates item
142
146
  if (valid) return [null, item]