11import type { type } from 'arktype' ;
22import {
33 type ValidationAdapter ,
4- type RequiredDefaultsOptions ,
54 createAdapter ,
65 type ClientValidationAdapter ,
76 type ValidationResult ,
8- createJsonSchema
7+ type AdapterOptions ,
8+ type Infer
99} from './adapters.js' ;
1010import { memoize } from '$lib/memoize.js' ;
11+ import type { JSONSchema7 } from 'json-schema' ;
12+
13+ type Options = Parameters < type . Any [ 'toJsonSchema' ] > [ 0 ] ;
1114
1215async function modules ( ) {
1316 const { type } = await import ( /* webpackIgnore: true */ 'arktype' ) ;
@@ -16,6 +19,23 @@ async function modules() {
1619
1720const fetchModule = /* @__PURE__ */ memoize ( modules ) ;
1821
22+ const defaultJSONSchemaOptions = {
23+ fallback : {
24+ default : ( ctx ) => ctx . base ,
25+ date : ( ctx ) => ( {
26+ ...ctx . base ,
27+ type : 'string' ,
28+ format : 'date-time' ,
29+ description : ctx . after ? `after ${ ctx . after } ` : 'anytime'
30+ } )
31+ }
32+ } satisfies Options ;
33+
34+ /* @__NO_SIDE_EFFECTS__ */
35+ export const arktypeToJSONSchema = < S extends type . Any > ( schema : S , options ?: Options ) => {
36+ return schema . toJsonSchema ( { ...defaultJSONSchemaOptions , ...options } ) as JSONSchema7 ;
37+ } ;
38+
1939async function _validate < T extends type . Any > (
2040 schema : T ,
2141 data : unknown
@@ -40,12 +60,12 @@ async function _validate<T extends type.Any>(
4060
4161function _arktype < T extends type . Any > (
4262 schema : T ,
43- options : RequiredDefaultsOptions < T [ 'infer' ] >
63+ options ?: AdapterOptions < Infer < T , 'arktype' > > & { config ?: Options }
4464) : ValidationAdapter < T [ 'infer' ] , T [ 'inferIn' ] > {
4565 return createAdapter ( {
4666 superFormValidationLibrary : 'arktype' ,
47- defaults : options . defaults ,
48- jsonSchema : createJsonSchema ( options ) ,
67+ defaults : options ? .defaults ,
68+ jsonSchema : options ?. jsonSchema ?? arktypeToJSONSchema ( schema , options ?. config ) ,
4969 validate : async ( data ) => _validate < T > ( schema , data )
5070 } ) ;
5171}
0 commit comments