rjsf-validator-cfworker
v1.0.2
Published
The @cfworker/json-schema based validator for @rjsf/core
Maintainers
Readme
rjsf-validator-cfworker
Sponsors
Development of this library has been sponsored by Glama.ai.
About The Project
Exports validator-cfworker plugin for react-jsonschema-form. This validator is based on @cfworker/json-schema, which is a lightweight JSON Schema validator optimized for Cloudflare Workers and other edge computing environments.
Why This Library?
This library was created to address a fundamental limitation with AJV (Another JSON Schema Validator): AJV cannot be used in contexts that disallow unsafe-eval.
Many modern web environments enforce strict Content Security Policies (CSP) that prohibit the use of eval() and similar dynamic code execution methods. This includes:
- Cloudflare Workers - Edge computing environment with strict security policies
- Browser extensions - Chrome and Firefox extensions with CSP restrictions
- Strict CSP web applications - Applications that set
script-srcwithout'unsafe-eval'
The original issue was raised in react-jsonschema-form#3269, requesting an alternative validator that works in these restricted environments.
This library provides a drop-in replacement for @rjsf/validator-ajv8 that uses @cfworker/json-schema under the hood, which does not rely on dynamic code evaluation.
Built With
Key Features
- Cloudflare Workers Compatible: Designed to work in edge computing environments without relying on
evalor dynamic code execution - Lightweight: Smaller bundle size compared to AJV-based validators
- JSON Schema Support: Supports JSON Schema drafts 4, 6, 7, 2019-09, and 2020-12
- No Precompilation Required: Works without code generation or precompilation steps
- CSP Compliant: Works in environments with strict Content Security Policies
Getting Started
Prerequisites
@rjsf/utils >= 5.0.0
Installation
npm install rjsf-validator-cfworker
# or
yarn add rjsf-validator-cfworkerUsage
import { RJSFSchema } from '@rjsf/utils';
import Form from '@rjsf/core';
import validator from 'rjsf-validator-cfworker';
const schema: RJSFSchema = {
type: 'string',
};
<Form schema={schema} validator={validator} />;or, using a custom validator with a specific JSON Schema draft version:
import { RJSFSchema } from '@rjsf/utils';
import Form from '@rjsf/core';
import { customizeValidator } from 'rjsf-validator-cfworker';
const validator = customizeValidator({
draft: '2020-12', // Use JSON Schema draft 2020-12
});
const schema: RJSFSchema = {
type: 'string',
};
<Form schema={schema} validator={validator} />;or, using short-circuit validation (stops on first error):
import { RJSFSchema } from '@rjsf/utils';
import Form from '@rjsf/core';
import { customizeValidator } from 'rjsf-validator-cfworker';
const validator = customizeValidator({
shortCircuit: true, // Stop validation on first error
});
const schema: RJSFSchema = {
type: 'string',
};
<Form schema={schema} validator={validator} />;or, combining options:
import { RJSFSchema } from '@rjsf/utils';
import Form from '@rjsf/core';
import { customizeValidator } from 'rjsf-validator-cfworker';
const validator = customizeValidator({
draft: '2019-09',
shortCircuit: false,
customFormats: {
'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/,
},
});
const schema: RJSFSchema = {
type: 'string',
format: 'phone-us',
};
<Form schema={schema} validator={validator} />;API
customizeValidator(options)
Creates a customized validator instance.
Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| draft | '4' \| '6' \| '7' \| '2019-09' \| '2020-12' | '2019-09' | The JSON Schema draft version to use |
| shortCircuit | boolean | false | Whether to stop validation on the first error |
| customFormats | object | undefined | Custom format validators (Note: limited support compared to AJV) |
Differences from @rjsf/validator-ajv8
This validator is designed for edge computing environments and has some differences from the AJV-based validator:
- No precompilation support: Unlike AJV, @cfworker/json-schema doesn't support standalone code generation
- Simplified format support: Custom formats are supported but with more limited functionality
- No meta schema customization: Additional meta schemas cannot be added dynamically
- No AJV-specific options: Options like
$data,verbose, anddiscriminatorare not available - No localization: The
localizerparameter is not supported
License
Distributed under the MIT License. See LICENSE for more information.
