cfworker/json-schema
Form validator implementation based on @cfworker/json-schema.
Installation
Section titled “Installation”npm i @sjsf/cfworker-validator@next @cfworker/json-schema
yarn add @sjsf/cfworker-validator@next @cfworker/json-schema
pnpm add @sjsf/cfworker-validator@next @cfworker/json-schema
bun add @sjsf/cfworker-validator@next @cfworker/json-schema
Example
Section titled “Example”<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>
import type { Schema, UiSchema } from "@sjsf/form";
export const schema: Schema = { type: "object", properties: { id: { type: "string", minLength: 8, pattern: "\\d+", }, active: { type: "boolean", }, skills: { type: "array", minItems: 4, items: { type: "string", minLength: 5, }, }, multipleChoicesList: { type: "array", maxItems: 2, items: { type: "string", enum: ["foo", "bar", "fuzz"], }, }, },};
export const uiSchema: UiSchema = { id: { "ui:options": { title: "Identifier", }, }, active: { "ui:options": { title: "Active", }, }, multipleChoicesList: { "ui:options": { title: "Pick max two items", }, },};
export const initialValue = { id: "Invalid", skills: ["karate", "budo", "aikido"], multipleChoicesList: ["foo", "bar", "fuzz"],};
Caveats
Section titled “Caveats”-
data-url
andcolor
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)): vIt 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 requred
. This can be fixed using error transformation.
Async validation
Section titled “Async validation”This validator does not support async validation.