返回资讯中心
外部精选
软件工程
#开发者实时流

Show HN: React-jsonschema-UI – JSON Schema forms for React Native and the web

JSON Schema forms for React Native (iOS / Android via Expo) and React on the web, from one

Hacker News 最新vyshakhp2026/10/20 · 26 天前14 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

JSON Schema forms for React Native (iOS / Android via Expo) and React on the web, from one component. Rendered with Tamagui, with state in Zustand and validation by Ajv.

npm install react-jsonschema-ui tamagui

Your app provides react (19+ when using the Tamagui widgets), tamagui (^2.7) and, on native, react-native / Expo. Ajv, ajv-formats and Zustand come with the package.

Render <SchemaForm> inside your app's <TamaguiProvider>:

Prop Description

schema JSON Schema (draft-07 / 2019-09 / 2020-12 keywords).

uiSchema Presentation hints, see below.

initialValues Starting values, merged with schema defaults.

onSubmit(values) Called with valid values.

onError(errors) Called when a submit fails validation, with { [pointer]: messages }.

onChange(values) Called on every change.

validateMode 'onBlur' (default), 'onChange' or 'onSubmit', before the first submit.

reValidateMode 'onChange' (default) or 'onBlur', after the first submit.

registry Extra or replacement widgets, fields and templates.

validator A custom validator (default: Ajv with ajv-formats).

store An external store from createFormStore, to read or drive the form from outside.

disabled, readOnly Apply to every field.

idPrefix Prefix for element ids (default root, e.g. root_address_city).

submitLabel / children Label of the default submit button, or replace it (null hides it).

Errors are shown per field: in the default onBlur mode a field is validated when it loses focus, then live while it has errors; submitting validates everything.

Directive Effect

ui:widget Pick a widget by name (see below).

ui:field Replace the whole field with a registered field component.

ui:title, ui:description, ui:help, ui:placeholder Text shown around the input.

ui:order Property order; '*' stands for the rest.

ui:enumNames Labels for enum values.

ui:hidden, ui:disabled, ui:readonly, ui:autofocus Field state.

ui:options Widget options, e.g. { rows: 5 } for textareas, { inline: true } for radios, { addable, removable, orderable } for arrays.

Nested properties use nested keys, and array items use items: { tasks: { items: { details: { 'ui:widget': 'textarea' } } } }.

Widget Chosen for

text, email, url, password, textarea string (by format / ui:widget)

date, datetime, time format: date / date-time / time (values stay valid RFC 3339)

number, range number, integer

checkbox, switch boolean

select, radio enum, or oneOf of { const, title }

checkboxes arrays of enum values with uniqueItems

On native, select opens a bottom sheet. Date and time widgets are text inputs on native; register a native picker as a custom widget if you want one.

A widget receives the field's value and callbacks, never the store:

import type { WidgetProps } from 'react-jsonschema-ui'; import { Button, XStack } from 'tamagui';

function RatingWidget({ value, onChange, onBlur, node }: WidgetProps<number | undefined>) { const max = node.schema.maximum ?? 5; return ( <XStack> {Array.from({ length: max }, (_, i) => ( <Button key={i} chromeless onPress={() => { onChange(i + 1); onBlur(); }}> {i < (value ?? 0) ? '★' : '☆'} </Button> ))} </XStack> ); }

<SchemaForm schema={schema} uiSchema={{ stars: { 'ui:widget': 'rating' } }} registry={{ widgets: { rating: RatingWidget } }} />;

Type your widget names by augmenting WidgetValueMap:

declare module 'react-jsonschema-ui' { interface WidgetValueMap { rating: number | undefined; } }

Layout is overridable too: FormTemplate, FieldTemplate, ObjectTemplate, ArrayTemplate, ArrayItemTemplate, SubmitButton and UnsupportedField can all be replaced through registry.templates.

react-jsonschema-ui/core contains the parser, validator, store and React bindings without any Tamagui import. Build a form with your own components:

import { SchemaForm, useField } from 'react-jsonschema-ui/core';

<SchemaForm schema={schema} registry={{ widgets: myWidgets, templates: myTemplates }} />;

Or skip the renderer and use the store and hooks directly — createFormStore, useField(pointer), useArrayField(pointer) and useFormSelector — with fields addressed by JSON Pointer (/address/city, /tasks/0/title).

Widgets use theme tokens ($background, $borderColor, $color10, $placeholderColor) and follow your app's light/dark themes. The defaults from @tamagui/config work out of the box.

The @tamagui/config/v5 preset only allows shorthand style props in its types. The library uses longhand props (they work with every config); if you type-check this package's source, set settings: { ...defaultConfig.settings, onlyAllowShorthands: false }.

JSON Schema support

Supported: type (including ['x', 'null']), properties, required, enum, const, default, items, minItems / maxItems, uniqueItems, string and number constraints, format (via ajv-formats), local $ref ($defs / definitions, recursive schemas), allOf, oneOf / anyOf of const values as labelled options.

Validated but not yet rendered specially: oneOf / anyOf sub-schema selection, if / then / else, dependentSchemas / dependencies, tuple items / prefixItems, additionalProperties.

Validation uses Ajv, which works on Hermes (verified on iOS with Expo SDK 57). If your runtime forbids new Function, precompile with Ajv standalone code and pass fromAjvValidateFunction(validate) as validator.

pnpm install pnpm test # unit, render and type tests pnpm typecheck pnpm lint pnpm build pnpm docs # playground at http://localhost:5173 pnpm --filter example start # Expo dev server

The playground and the Expo app compile the library from source, so changes show up without a rebuild.

Hi there! I'm Vyshakh Parakkat. My expertise lies in crafting robust solutions using React Native, ReactJS, Kubernetes and Python.

If you have any questions, suggestions, or just want to connect, feel free to reach out.

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →