Skip to content

Commit 894c4c4

Browse files
committed
[sveltekit] Extend AdditionalPropertyKeyValidationError function options
1 parent 67b5996 commit 894c4c4

File tree

5 files changed

+69
-33
lines changed

5 files changed

+69
-33
lines changed

packages/sveltekit/src/lib/client/create-form.svelte.ts

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { 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';
310
import {
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

2430
import 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

3348
export type AdditionalPropertyKeyValidationError2 =
3449
| string
35-
| ((ctx: { key: string; separator: string; separators: string[] }) => string);
50+
| ((ctx: AdditionalPropertyKeyValidationErrorFnOptions) => string);
3651

3752
export 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;

packages/sveltekit/src/routes/+page.server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export const actions = {
4646
request,
4747
schema
4848
});
49-
console.log(data);
5049
return {
5150
form: await validateForm2({
5251
request,
@@ -57,7 +56,7 @@ export const actions = {
5756
})
5857
};
5958
},
60-
register: async () => {
59+
second: async () => {
6160
return {
6261
form2: validateForm({
6362
schema,

packages/sveltekit/src/routes/form1.svelte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<script lang="ts">
32
import { Content, setFromContext } from '@sjsf/form';
43
import { createValidator2 } from '@sjsf/ajv8-validator';
@@ -9,6 +8,8 @@
98
109
import type { PageData, ActionData } from './$types.js';
1110
11+
import { ERROR_TYPE_OBJECTS } from './model.js';
12+
1213
const meta = createMeta<ActionData, PageData, 'form'>('form');
1314
const request = createSvelteKitRequest(meta, {
1415
onSuccess: console.log,
@@ -20,9 +21,9 @@
2021
translation,
2122
onSubmit: request.run,
2223
onSubmitError: console.warn,
23-
additionalPropertyKeyValidationError({ separators }) {
24-
return `The content of these sequences ("${separators.join('", "')}") is prohibited`;
25-
},
24+
additionalPropertyKeyValidationError({ type, values }) {
25+
return `The presence of these ${ERROR_TYPE_OBJECTS[type]} ("${values.join('", "')}") is prohibited`;
26+
}
2627
});
2728
setFromContext(form.context);
2829
</script>

packages/sveltekit/src/routes/form2.svelte

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11

22
<script lang="ts">
3-
import { Content, RawForm, setFromContext } from '@sjsf/form';
3+
import { RawForm, setFromContext } from '@sjsf/form';
44
import { createValidator2 } from '@sjsf/ajv8-validator';
55
import { theme } from '@sjsf/form/basic-theme';
66
import { translation } from '@sjsf/form/translations/en';
77
88
import { createMeta, createSvelteKitForm, createSvelteKitRequest } from '$lib/client/index.js';
99
1010
import type { PageData, ActionData } from './$types.js';
11+
12+
import { ERROR_TYPE_OBJECTS } from './model.js';
1113
1214
const meta = createMeta<ActionData, PageData, 'form2'>('form2');
1315
const request = createSvelteKitRequest(meta, {
@@ -31,11 +33,11 @@
3133
translation,
3234
onSubmit: request.run,
3335
onSubmitError: console.warn,
34-
additionalPropertyKeyValidationError({ separators }) {
35-
return `The content of these sequences ("${separators.join('", "')}") is prohibited`;
36+
additionalPropertyKeyValidationError({ type, values }) {
37+
return `The presence of these ${ERROR_TYPE_OBJECTS[type]} ("${values.join('", "')}") is prohibited`;
3638
},
3739
});
3840
setFromContext(form.context);
3941
</script>
4042

41-
<RawForm {form} />
43+
<RawForm {form} method="POST" action="?/second" />
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { AdditionalPropertyKeyValidationErrorType } from '$lib/client/index.js';
2+
3+
export const ERROR_TYPE_OBJECTS: Record<AdditionalPropertyKeyValidationErrorType, string> = {
4+
[AdditionalPropertyKeyValidationErrorType.ForbiddenSequence]: 'sequences',
5+
[AdditionalPropertyKeyValidationErrorType.ForbiddenSuffix]: 'suffixes'
6+
};

0 commit comments

Comments
 (0)