Skip to content

Commit 67b5996

Browse files
committed
[form] Pass object schema to the AdditionalPropertyKeyValidator
1 parent 09dce24 commit 67b5996

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
lines changed

packages/form/src/form/context/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface FormContext {
4343
IconOrTranslation: Component<{ data: IconOrTranslationData }>;
4444
/** @deprecated use `IconOrTranslation` instead */
4545
iconOrTranslation: Snippet<[IconOrTranslationData]>;
46-
validateAdditionalPropertyKey(config: Config, key: string): boolean;
46+
validateAdditionalPropertyKey(config: Config, key: string, fieldConfig: Config): boolean;
4747
validation: Mutation<
4848
[event: SubmitEvent],
4949
{

packages/form/src/form/create-form.svelte.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,17 @@ export function createForm3<
340340
const additionalPropertyKeyValidator = $derived.by(() => {
341341
const validator = options.additionalPropertyKeyValidator;
342342
return validator
343-
? (config: Config, key: string) => {
344-
const instanceId = config.idSchema.$id;
345-
const messages = validator.validateAdditionalPropertyKey(key, config);
343+
? (config: Config, key: string, fieldConfig: Config) => {
344+
const instanceId = fieldConfig.idSchema.$id;
345+
const messages = validator.validateAdditionalPropertyKey(
346+
key,
347+
config.schema
348+
);
346349
errors.set(
347350
instanceId,
348351
messages.map((message) => ({
349352
instanceId,
350-
propertyTitle: config.title,
353+
propertyTitle: fieldConfig.title,
351354
message,
352355
error: ADDITIONAL_PROPERTY_KEY_ERROR as E,
353356
}))

packages/form/src/form/fields/object/context.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { getContext, setContext } from "svelte";
22

3+
import type { Config } from '../../config.js';
4+
35
export interface ObjectContext {
46
/** @deprecated */
57
newKeySeparator: string;
68
/** @deprecated */
79
validate: () => void;
810
addProperty(): void
9-
renameProperty(oldProp: string, newProp: string): void
11+
renameProperty(oldProp: string, newProp: string, config: Config): void
1012
removeProperty(prop: string): void
1113
}
1214

packages/form/src/form/fields/object/object-field.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@
7272
delete value[prop];
7373
validate();
7474
},
75-
renameProperty(oldProp, newProp) {
75+
renameProperty(oldProp, newProp, fieldConfig) {
7676
if (value === undefined) {
7777
return;
7878
}
7979
const newKey = generateNewKey(newProp, newKeySeparator, value);
80-
if (!ctx.validateAdditionalPropertyKey(config, newKey)) {
80+
if (!ctx.validateAdditionalPropertyKey(config, newKey, fieldConfig)) {
8181
return;
8282
}
8383
value[newKey] = value[oldProp];

packages/form/src/form/fields/object/object-key-input.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { proxy } from "@/lib/svelte.svelte";
3-
import { type SchemaObjectValue, type SchemaValue } from "@/core/index.js";
3+
import type { SchemaValue } from "@/core/index.js";
44
55
import type { Config } from "../../config.js";
66
import type { UiSchema } from "../../ui-schema.js";
@@ -57,7 +57,7 @@
5757
if (!key.value || key.value === property) {
5858
return;
5959
}
60-
objCtx.renameProperty(property, key.value)
60+
objCtx.renameProperty(property, key.value, config);
6161
},
6262
})
6363
);

packages/form/src/form/validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export interface AdditionalPropertyKeyValidator {
7171
/**
7272
* Additional property key validation
7373
*/
74-
validateAdditionalPropertyKey: (key: string, config: Config) => string[];
74+
validateAdditionalPropertyKey: (key: string, schema: Schema) => string[];
7575
}
7676

7777
export const VALIDATION_PROCESS_ERROR = Symbol("validation-process-error");

0 commit comments

Comments
 (0)