@@ -2,8 +2,8 @@ import { memoize } from '$lib/memoize.js';
22import {
33 createAdapter ,
44 type AdapterOptions ,
5- type ValidationAdapter ,
6- type ValidationResult
5+ type ClientValidationAdapter ,
6+ type ValidationAdapter
77} from './adapters.js' ;
88import {
99 validator ,
@@ -15,60 +15,103 @@ import {
1515import type { FromSchema , JSONSchema } from 'json-schema-to-ts' ;
1616import type { JSONSchema as JSONSchema7 } from '$lib/jsonSchema/index.js' ;
1717
18+ /*
19+ * Adapter specificts:
20+ * Type inference problem unless this is applied:
21+ * https://github.com/ThomasAribart/json-schema-to-ts/blob/main/documentation/FAQs/applying-from-schema-on-generics.md
22+ * Must duplicate validate method, otherwise the above type inference will fail.
23+ */
24+
1825const Email =
1926 / ^ [ a - z 0 - 9 ! # $ % & ' * + / = ? ^ _ ` { | } ~ - ] + (?: \. [ a - z 0 - 9 ! # $ % & ' * + / = ? ^ _ ` { | } ~ - ] + ) * @ (?: [ a - z 0 - 9 ] (?: [ a - z 0 - 9 - ] * [ a - z 0 - 9 ] ) ? \. ) + [ a - z 0 - 9 ] (?: [ a - z 0 - 9 - ] * [ a - z 0 - 9 ] ) ? $ / i;
2027
21- // Type inference problem unless this is applied: https://github.com/ThomasAribart/json-schema-to-ts/blob/main/documentation/FAQs/applying-from-schema-on-generics.md
28+ const defaultOptions = {
29+ formats : {
30+ email : ( str : string ) => Email . test ( str )
31+ } ,
32+ includeErrors : true ,
33+ allErrors : true
34+ } ;
35+
36+ function cachedValidator ( currentSchema : JSONSchema7 , config ?: ValidatorOptions ) {
37+ if ( ! cache . has ( currentSchema ) ) {
38+ cache . set (
39+ currentSchema ,
40+ validator ( currentSchema as Schema , {
41+ ...defaultOptions ,
42+ ...config
43+ } )
44+ ) ;
45+ }
46+
47+ return cache . get ( currentSchema ) ! ;
48+ }
2249
2350function _schemasafe <
2451 T extends JSONSchema | Record < string , unknown > ,
25- Data = unknown extends FromSchema < T > ? Record < string , unknown > : FromSchema < T >
52+ Data = unknown extends FromSchema < T > ? Record < string , unknown > : FromSchema < T > ,
53+ Out = [ Data ] extends [ never ] ? Record < string , unknown > : Data
2654> (
2755 schema : T ,
28- options ?: AdapterOptions < Data > & { config ?: ValidatorOptions }
29- ) : ValidationAdapter < Data > {
56+ options ?: AdapterOptions < Out > & { config ?: ValidatorOptions }
57+ ) : ValidationAdapter < Out > {
3058 return createAdapter ( {
3159 superFormValidationLibrary : 'schemasafe' ,
3260 jsonSchema : schema as JSONSchema7 ,
3361 defaults : options ?. defaults ,
34- async validate ( data : unknown ) : Promise < ValidationResult < Data > > {
35- const currentSchema = schema as JSONSchema7 ;
62+ async validate ( data : unknown ) {
63+ const validator = cachedValidator ( schema as JSONSchema7 , options ?. config ) ;
64+ const isValid = validator ( data as Json ) ;
3665
37- if ( ! cache . has ( currentSchema ) ) {
38- cache . set (
39- currentSchema ,
40- validator ( currentSchema as Schema , {
41- formats : {
42- email : ( str ) => Email . test ( str )
43- } ,
44- includeErrors : true ,
45- allErrors : true ,
46- ...options ?. config
47- } )
48- ) ;
66+ if ( isValid ) {
67+ return {
68+ data : data as Out ,
69+ success : true
70+ } ;
4971 }
72+ return {
73+ issues : ( validator . errors ?? [ ] ) . map ( ( { instanceLocation, keywordLocation } ) => ( {
74+ message : keywordLocation ,
75+ path : instanceLocation . split ( '/' ) . slice ( 1 )
76+ } ) ) ,
77+ success : false
78+ } ;
79+ }
80+ } ) ;
81+ }
5082
51- const _validate = cache . get ( currentSchema ) ! ;
52-
53- const isValid = _validate ( data as Json ) ;
83+ function _schemasafeClient <
84+ T extends JSONSchema | Record < string , unknown > ,
85+ Data = unknown extends FromSchema < T > ? Record < string , unknown > : FromSchema < T > ,
86+ Out = [ Data ] extends [ never ] ? Record < string , unknown > : Data
87+ > (
88+ schema : T ,
89+ options ?: AdapterOptions < Out > & { config ?: ValidatorOptions }
90+ ) : ClientValidationAdapter < Out > {
91+ return {
92+ superFormValidationLibrary : 'schemasafe' ,
93+ async validate ( data : unknown ) {
94+ const validator = cachedValidator ( schema as JSONSchema7 , options ?. config ) ;
95+ const isValid = validator ( data as Json ) ;
5496
5597 if ( isValid ) {
5698 return {
57- data : data as Data ,
99+ data : data as Out ,
58100 success : true
59101 } ;
60102 }
61103 return {
62- issues : ( _validate . errors ?? [ ] ) . map ( ( { instanceLocation, keywordLocation } ) => ( {
104+ issues : ( validator . errors ?? [ ] ) . map ( ( { instanceLocation, keywordLocation } ) => ( {
63105 message : keywordLocation ,
64106 path : instanceLocation . split ( '/' ) . slice ( 1 )
65107 } ) ) ,
66108 success : false
67109 } ;
68110 }
69- } ) ;
111+ } ;
70112}
71113
72114export const schemasafe = /* @__PURE__ */ memoize ( _schemasafe ) ;
115+ export const schemasafeClient = /* @__PURE__ */ memoize ( _schemasafeClient ) ;
73116
74117const cache = new WeakMap < JSONSchema7 , Validate > ( ) ;
0 commit comments