11/* eslint-disable @typescript-eslint/no-explicit-any */
22import { isRecord } from '@sjsf/form/lib/object' ;
3+ import { some } from '@sjsf/form/lib/array' ;
4+ import {
5+ isArrayOrObjectSchemaType ,
6+ isPrimitiveSchemaType ,
7+ isSchema ,
8+ typeOfSchema
9+ } from '@sjsf/form/core' ;
310import {
411 DEFAULT_ID_SEPARATOR ,
512 DEFAULT_PSEUDO_ID_SEPARATOR ,
@@ -9,7 +16,6 @@ import {
916 type Schema ,
1017 type FormOptions ,
1118 groupErrors ,
12- type Config ,
1319 type IdentifiableFieldElement
1420} from '@sjsf/form' ;
1521
@@ -22,17 +28,26 @@ import {
2228} from '../model.js' ;
2329
2430import type { SvelteKitFormMeta } from './meta.js' ;
25- import {
26- isArrayOrObjectSchemaType ,
27- isPrimitiveSchemaType ,
28- isSchema ,
29- typeOfSchema
30- } from '@sjsf/form/core' ;
31- import { some } from '@sjsf/form/lib/array' ;
31+
32+ export enum AdditionalPropertyKeyValidationErrorType {
33+ ForbiddenSequence = 'forbidden-sequence' ,
34+ ForbiddenSuffix = 'forbidden-suffix'
35+ }
36+
37+ export interface AdditionalPropertyKeyValidationErrorFnOptions {
38+ key : string ;
39+ type : AdditionalPropertyKeyValidationErrorType ;
40+ /** @deprecated */
41+ separator : string ;
42+ /** @deprecated */
43+ separators : string [ ] ;
44+ value : string ;
45+ values : string [ ] ;
46+ }
3247
3348export type AdditionalPropertyKeyValidationError2 =
3449 | string
35- | ( ( ctx : { key : string ; separator : string ; separators : string [ ] } ) => string ) ;
50+ | ( ( ctx : AdditionalPropertyKeyValidationErrorFnOptions ) => string ) ;
3651
3752export type SvelteKitFormOptions2 < V , E , SendSchema extends boolean > = Omit <
3853 FormOptions < V , E > ,
@@ -89,12 +104,29 @@ export function createSvelteKitForm<
89104 additionalPropertyKeyValidator :
90105 additionalPropertyKeyValidationError !== undefined
91106 ? {
92- validateAdditionalPropertyKey ( key : string , config : Config ) : string [ ] {
107+ validateAdditionalPropertyKey ( key : string , { additionalProperties } : Schema ) : string [ ] {
93108 const messages : string [ ] = [ ] ;
94- const { additionalProperties } = config . schema ;
95109 if ( additionalProperties === undefined ) {
96110 return messages ;
97111 }
112+ const pushMessage = (
113+ type : AdditionalPropertyKeyValidationErrorType ,
114+ value : string ,
115+ values : string [ ]
116+ ) =>
117+ messages . push (
118+ typeof additionalPropertyKeyValidationError === 'string'
119+ ? additionalPropertyKeyValidationError
120+ : additionalPropertyKeyValidationError ( {
121+ type,
122+ key,
123+ value,
124+ values,
125+ separator : value ,
126+ separators : values
127+ } )
128+ ) ;
129+ // TODO: handle `$ref` in `additionalProperties`
98130 const types = isSchema ( additionalProperties )
99131 ? typeOfSchema ( additionalProperties )
100132 : [ ] ;
@@ -107,24 +139,20 @@ export function createSvelteKitForm<
107139 continue ;
108140 }
109141 }
110- messages . push (
111- typeof additionalPropertyKeyValidationError === 'string'
112- ? additionalPropertyKeyValidationError
113- : additionalPropertyKeyValidationError ( { key , separator , separators } )
142+ pushMessage (
143+ AdditionalPropertyKeyValidationErrorType . ForbiddenSequence ,
144+ separator ,
145+ separators
114146 ) ;
115147 }
116148 for ( const suffix of suffixes ) {
117149 if ( ! key . endsWith ( suffix ) || ! some ( types , isPrimitiveSchemaType ) ) {
118150 continue ;
119151 }
120- messages . push (
121- typeof additionalPropertyKeyValidationError === 'string'
122- ? additionalPropertyKeyValidationError
123- : additionalPropertyKeyValidationError ( {
124- key,
125- separator : suffix ,
126- separators : suffixes
127- } )
152+ pushMessage (
153+ AdditionalPropertyKeyValidationErrorType . ForbiddenSuffix ,
154+ suffix ,
155+ suffixes
128156 ) ;
129157 }
130158 return messages ;
0 commit comments