cfworker/json-schema
Form validator implementation based on @cfworker/json-schema.
Installation
Section titled “Installation”npm i @sjsf/cfworker-validatorpnpm add @sjsf/cfworker-validatoryarn add @sjsf/cfworker-validatorbun add @sjsf/cfworker-validatordeno add @sjsf/cfworker-validatorRequired peer dependencies: @cfworker/json-schema@4.1.0 @sjsf/form@3.5.0
Example
Section titled “Example”<script lang="ts">
import { createFormValidator } from "@sjsf/cfworker-validator";
import {
BasicForm,
createForm,
getValueSnapshot,
ON_ARRAY_CHANGE,
ON_CHANGE,
ON_INPUT,
} from "@sjsf/form";
import * as defaults from "$lib/sjsf/defaults";
import { initialValue, schema, uiSchema } from "../demo-schema";
const form = createForm({
...defaults,
schema,
uiSchema,
validator: createFormValidator,
fieldsValidationMode: ON_INPUT | ON_CHANGE | ON_ARRAY_CHANGE,
initialValue,
});
</script>
<BasicForm {form} novalidate />
<pre>{JSON.stringify(getValueSnapshot(form), null, 2)}</pre>
Caveats
Section titled “Caveats”-
data-urlandcolorformats are ignored. -
By itself this validator does not support undefined values. But we use the
valueToJSONfunction with the following default implementation to fix it, consider this overhead.const valueToJSON = (v: FormValue) =>v === undefined || v === null? null: typeof v === "object"? JSON.parse(JSON.stringify(v)): v;It will also lead to incorrect error text (e.g.
Instance type "null" is invalid. Expected "string") where you would like to see the textthis field is required. This can be fixed using error transformation.
Async validation
Section titled “Async validation”This validator does not support async validation.