@@ -8,6 +8,7 @@ import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib/json-s
88import { JsonSchemaAnyBuilder } from '@naturalcycles/js-lib/json-schema'
99import { _filterNullishValues } from '@naturalcycles/js-lib/object'
1010import { _substringBefore } from '@naturalcycles/js-lib/string'
11+ import { z , type ZodType } from '@naturalcycles/js-lib/zod'
1112import type { Ajv } from 'ajv'
1213import { _inspect } from '../../string/inspect.js'
1314import { AjvValidationError } from './ajvValidationError.js'
@@ -55,8 +56,6 @@ export interface AjvSchemaCfg {
5556/**
5657 * On creation - compiles ajv validation function.
5758 * Provides convenient methods, error reporting, etc.
58- *
59- * @experimental
6059 */
6160export class AjvSchema < T = unknown > {
6261 private constructor (
@@ -81,7 +80,7 @@ export class AjvSchema<T = unknown> {
8180 */
8281 static createLazy < T > (
8382 schema : JsonSchemaBuilder < T > | JsonSchema < T > | AjvSchema < T > ,
84- cfg : Partial < AjvSchemaCfg > = { } ,
83+ cfg ? : Partial < AjvSchemaCfg > ,
8584 ) : AjvSchema < T > {
8685 return AjvSchema . create ( schema , {
8786 lazy : true ,
@@ -100,7 +99,7 @@ export class AjvSchema<T = unknown> {
10099 */
101100 static create < T > (
102101 schema : JsonSchemaBuilder < T > | JsonSchema < T > | AjvSchema < T > ,
103- cfg : Partial < AjvSchemaCfg > = { } ,
102+ cfg ? : Partial < AjvSchemaCfg > ,
104103 ) : AjvSchema < T > {
105104 if ( schema instanceof AjvSchema ) return schema
106105 if ( schema instanceof JsonSchemaAnyBuilder ) {
@@ -109,6 +108,11 @@ export class AjvSchema<T = unknown> {
109108 return new AjvSchema < T > ( schema as JsonSchema < T > , cfg )
110109 }
111110
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+
112116 readonly cfg : AjvSchemaCfg
113117
114118 /**
0 commit comments