The @cfworker/json-schema based validator for @rjsf/core
npm install rjsf-validator-cfworker[![npm][npm-shield]][npm-url]
[![npm downloads][npm-dl-shield]][npm-dl-url]
[![MIT License][license-shield]][license-url]
Development of this library has been sponsored by Glama.ai.
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.
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-src without '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.
- react-jsonschema-form
- @cfworker/json-schema
- TypeScript
- Cloudflare Workers Compatible: Designed to work in edge computing environments without relying on eval or 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
- @rjsf/utils >= 5.0.0
``bash`
npm install rjsf-validator-cfworkeror
yarn add rjsf-validator-cfworker
`tsx
import { RJSFSchema } from '@rjsf/utils';
import Form from '@rjsf/core';
import validator from 'rjsf-validator-cfworker';
const schema: RJSFSchema = {
type: 'string',
};
or, using a custom validator with a specific JSON Schema draft version:
`tsx
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',
};
;
`or, using short-circuit validation (stops on first error):
`tsx
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',
};
;
`or, combining options:
`tsx
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',
};
;
`API
$3
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) |$3
This validator is designed for edge computing environments and has some differences from the AJV-based validator:
1. No precompilation support: Unlike AJV, @cfworker/json-schema doesn't support standalone code generation
2. Simplified format support: Custom formats are supported but with more limited functionality
3. No meta schema customization: Additional meta schemas cannot be added dynamically
4. No AJV-specific options: Options like
$data, verbose, and discriminator are not available
5. No localization: The localizer parameter is not supportedLicense
Distributed under the MIT License. See
LICENSE` for more information.[license-shield]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
[license-url]: https://github.com/glama-ai/rjsf-validator-cfworker/blob/main/LICENSE
[npm-shield]: https://img.shields.io/npm/v/rjsf-validator-cfworker/latest.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/rjsf-validator-cfworker
[npm-dl-shield]: https://img.shields.io/npm/dm/rjsf-validator-cfworker.svg?style=flat-square
[npm-dl-url]: https://www.npmjs.com/package/rjsf-validator-cfworker