Skip to content
Playground

cfworker/json-schema

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

Installation

Terminal window
npm i @sjsf/cfworker-validator @cfworker/json-schema

Example

<script lang="ts">
import { RawForm, ON_INPUT } from "@sjsf/form";
import { createValidator } from "@sjsf/cfworker-validator";
import { createCustomForm } from "@/components/custom-form";
import { schema, uiSchema } from "./_shared";
const validator = createValidator({ uiSchema });
const form = createCustomForm({
schema,
uiSchema,
validator,
fieldsValidationMode: ON_INPUT,
initialValue: {
id: "Invalid",
skills: ["karate", "budo", "aikido"],
multipleChoicesList: ["foo", "bar", "fuzz"],
},
});
</script>
<RawForm {form} novalidate />
<pre>{JSON.stringify(form.value, null, 2)}</pre>

Async validation

This validator does not support async validation.

API

import type { Validator as CfValidator } from "@cfworker/json-schema";
import type { Schema, UiSchemaRoot, Config } from "@sjsf/form";
import type { Validator } from '@sjsf/cfworker-validator'
type CfValidatorFactory = (schema: Schema) => CfValidator;
interface ValidatorOptions {
factory: CfValidatorFactory;
/** @default {} */
uiSchema?: UiSchemaRoot;
/** @default DEFAULT_ID_PREFIX */
idPrefix?: string;
/** @default DEFAULT_ID_SEPARATOR */
idSeparator?: string;
/** @default makeValidatorFactory(factory) */
createValidator?: (schema: Schema, rootSchema: Schema) => CfValidator;
/** @default makeFieldValidatorFactory(factory) */
createFieldValidator?: (config: Config) => CfValidator;
}
type ValidatorFactoryOptions = Omit<ValidatorOptions, "factory"> & {
/** @default () => new CfValidator(schema, "7", false) */
factory?: CfValidatorFactory;
};
function createValidator(options?: ValidatorFactoryOptions): Validator