Skip to content

Commit 98601b8

Browse files
authored
make form less fragile (#134)
* Wrap experiment config form in error boundary * Replace unwrap() and structuredClone with JSON clone As unwrap sometimes returns proxy object which structuredClone cannot handle. Refs #150 * Revert "Wrap experiment config form in error boundary" This reverts commit 719a371.
1 parent ada3323 commit 98601b8

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

packages/form/src/Form.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ function useFormContext() {
112112
return value;
113113
}
114114

115+
function cloneAndUnwrap<T>(value: T): T {
116+
const s = JSON.stringify(value);
117+
const val = JSON.parse(s);
118+
return val;
119+
}
120+
115121
function createFormStore(
116122
schema: Accessor<JSONSchemaType<GenericConfig>>,
117123
initialValues: GenericConfig,
@@ -123,7 +129,7 @@ function createFormStore(
123129
schema: JSONSchemaType<GenericConfig>;
124130
}>({
125131
// Copy props.values as initial form values
126-
values: structuredClone(unwrap(initialValues)),
132+
values: cloneAndUnwrap(initialValues),
127133
errors: [],
128134
schema: schema(),
129135
});
@@ -138,7 +144,7 @@ function createFormStore(
138144
setStore("errors", errors);
139145
},
140146
reset: () => {
141-
setStore("values", structuredClone(initialValues));
147+
setStore("values", cloneAndUnwrap(initialValues));
142148
setStore("errors", []);
143149
},
144150
get errors() {
@@ -177,11 +183,16 @@ export function Form<C extends GenericConfig>(props: Props<C>) {
177183
: DEFAULT_UI_COMPONENTS;
178184
const schemaWithDefaults = createMemo(() => {
179185
// TODO do not cast, but without it ajv and solidjs complain
180-
const uschema = unwrap(props.schema as JSONSchemaType<GenericConfig>);
186+
const uschema = cloneAndUnwrap(
187+
props.schema as JSONSchemaType<GenericConfig>,
188+
);
181189
if (!props.defaults) {
182190
return uschema;
183191
}
184-
return overwriteDefaultsInJsonSchema(uschema, unwrap(props.defaults));
192+
return overwriteDefaultsInJsonSchema(
193+
uschema,
194+
cloneAndUnwrap(props.defaults),
195+
);
185196
});
186197
const validate = createMemo(() => buildValidate(schemaWithDefaults()));
187198
const store = createFormStore(schemaWithDefaults, props.values, uiComponents);

0 commit comments

Comments
 (0)