Skip to content
Playground Form Builder

Validator

Validator - a collection of functions used by the form to validate data:

  • isValid - required. Used to correctly handle conditional keywords such as oneOf, anyOf, and if/then/else.
  • validateFormValue - required unless validateFormValueAsync is defined.
  • validateFormValueAsync - required unless validateFormValue is defined.
  • validateFieldValue - optional
  • validateFieldValueAsync - optional
  • validateAdditionalPropertyKey - optional

You can easily extend/modify the validator to suit your needs.

import type { FormValueValidator } from "@sjsf/form";
import { createFormValidator } from "@sjsf/ajv8-validator";
export function validator() {
const validator = createFormValidator();
return {
...validator,
validateFormValue(rootSchema, formValue) {
const errors = validator.validateFormValue(rootSchema, formValue);
// Your logic
return errors
},
} satisfies FormValueValidator;
}
export interface Validator {
isValid(
schema: SchemaDefinition,
rootSchema: Schema,
formValue: SchemaValue | undefined
): boolean;
}
export interface ValidationError {
path: RPath;
message: string;
}
export interface SuccessValidationResult<Output> {
readonly value: Output;
readonly errors?: undefined;
}
export interface FailureValidationResult {
readonly value: FormValue;
readonly errors: ReadonlyArray<ValidationError>;
}
export type ValidationResult<Output> =
| SuccessValidationResult<Output>
| FailureValidationResult;
export interface FormValueValidator<Output> {
validateFormValue: (
rootSchema: Schema,
formValue: FormValue
) => ValidationResult<Output>;
}
export interface AsyncFormValueValidator<Output> {
validateFormValueAsync: (
signal: AbortSignal,
rootSchema: Schema,
formValue: FormValue
) => Promise<ValidationResult<Output>>;
}
export type AnyFormValueValidator<Output> =
| FormValueValidator<Output>
| AsyncFormValueValidator<Output>;
export type FormValidator<Output> = Validator & AnyFormValueValidator<Output>;
export type Update<T> = T | ((data: T) => T);
export interface FieldValueValidator {
validateFieldValue: (
field: Config,
fieldValue: FieldValue
) => Update<string[]>;
}
export interface AsyncFieldValueValidator {
validateFieldValueAsync: (
signal: AbortSignal,
field: Config,
fieldValue: FieldValue
) => Promise<Update<string[]>>;
}
export type AnyFieldValueValidator =
| FieldValueValidator
| AsyncFieldValueValidator;
export interface AdditionalPropertyKeyValidator {
validateAdditionalPropertyKey: (
key: string,
schema: Schema
) => Update<string[]>;
}
export interface AsyncFileListValidator {
validateFileListAsync: (
signal: AbortSignal,
fileList: FileList,
config: Config
) => Promise<Update<string[]>>;
}