JSPM

rjsf-validator-cfworker

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 453
  • Score
    100M100P100Q103616F
  • License MIT

The @cfworker/json-schema based validator for @rjsf/core

Package Exports

  • rjsf-validator-cfworker
  • rjsf-validator-cfworker/dist
  • rjsf-validator-cfworker/dist/customizeValidator.cjs
  • rjsf-validator-cfworker/dist/index.cjs
  • rjsf-validator-cfworker/dist/processRawValidationErrors.cjs
  • rjsf-validator-cfworker/dist/types.cjs
  • rjsf-validator-cfworker/dist/validator.cjs
  • rjsf-validator-cfworker/lib
  • rjsf-validator-cfworker/lib/customizeValidator.js
  • rjsf-validator-cfworker/lib/index.js
  • rjsf-validator-cfworker/lib/processRawValidationErrors.js
  • rjsf-validator-cfworker/lib/types.js
  • rjsf-validator-cfworker/lib/validator.js

Readme

rjsf-validator-cfworker

npm npm downloads MIT License

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-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.

Built With

Key Features

  • 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

Getting Started

Prerequisites

  • @rjsf/utils >= 5.0.0

Installation

npm install rjsf-validator-cfworker
# or
yarn add rjsf-validator-cfworker

Usage

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:

  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 supported

License

Distributed under the MIT License. See LICENSE for more information.