Skip to content
Playground

cfworker/json-schema

Form validator implementation based on @cfworker/json-schema.

Terminal window
npm i @sjsf/cfworker-validator@next @cfworker/json-schema
<script lang="ts">
import { BasicForm, ON_INPUT } from "@sjsf/form";
import { createFormValidator } from "@sjsf/cfworker-validator";
import { createMyForm } from "@/components/my-form";
import { initialValue, schema, uiSchema } from "../shared";
const validator = createFormValidator({ uiSchema });
const form = createMyForm({
schema,
uiSchema,
validator,
fieldsValidationMode: ON_INPUT,
initialValue
});
</script>
<BasicForm {form} novalidate />
<pre>{JSON.stringify(form.value, null, 2)}</pre>

  • data-url and color formats are ignored.

  • By itself this validator does not support undefined values. But we use the valueToJSON function 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 text this field is requred. This can be fixed using error transformation.

This validator does not support async validation.