Public
Likevalwidgets
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v191View latest version
Schema-driven widget engine for npm, Deno, and bindings (R via reactR, Python).
First version ships the Checkbox widget only.
npm
npm install dswidgetsengine
Deno
import * as widgets from "npm:dswidgetsengine";
import {
validateCheckbox,
getCheckboxDefaults,
getSchema,
applyDefaults,
createValidator,
} from "dswidgetsengine";
// Default config for a checkbox (then set a unique `id`)
const defaults = getCheckboxDefaults();
// Validate and normalize a config (applies schema defaults, then validates)
const result = validateCheckbox({
type: "checkbox",
id: "my-checkbox",
value: true,
});
if (result.valid) {
console.log(result.data); // full CheckboxConfig
} else {
console.log(result.errors);
}
// Load schema and validate generically
const schema = getSchema("checkbox", 1);
const validator = createValidator(schema);
const out = validator({ type: "checkbox", id: "x", value: false });
import { Checkbox } from "dswidgetsengine";
<Checkbox
id="agree"
label="I agree"
value={checked}
onChange={setChecked}
/>
Props match CheckboxConfig plus optional onChange(value: boolean).
Lee un JSON con varios widgets, aplica defaults y mantiene los valores actualizados:
import { WidgetForm, parseManifest, toManifestJSON } from "dswidgetsengine";
const manifest = {
widgets: [
{ type: "checkbox", id: "chk1", label: "Acepto", value: false },
{ type: "radio", id: "r1", label: "Opción", choices: [...], selected: "a" },
],
};
<WidgetForm
manifest={manifest}
onChange={(values, getManifestJSON) => {
console.log(values); // { chk1: true, r1: "b" }
console.log(getManifestJSON()); // JSON con valores actualizados
}}
/>
Formatos aceptados: { widgets: [...] }, [...] o string JSON. Los widgets siguen siendo usables por separado (<Checkbox ... />, <Radio ... />).
- getSchema(widgetType, version?) – load widget JSON Schema
- getRegisteredTypes() – list registered widget types
- getDefaultsFromSchema(schema) – default values from schema
- applyDefaults(schema, config) – merge config with schema defaults
- createValidator(schema) – compile validator for a schema
- validate(schema, data) – validate data against schema
- validateCheckbox(config) – validate and normalize checkbox config
- getCheckboxDefaults() – default CheckboxConfig
- Checkbox – React component for checkbox widget
- parseManifest(input) – parse and validate manifest JSON
- getDefaultValues(manifest) – extract default values from manifest
- manifestWithValues(manifest, values) – manifest with updated values
- toManifestJSON(manifest, values) – serialize manifest as JSON
Esta rama está enfocada solo en despliegue para Val Town.
| Comando | Salida | Uso |
|---|---|---|
npm run build:backend | bundle/backend.js | Runtime backend para validación y manifest |
npm run build:ui | bundle/ui.js | Bundle React para render en browser |
npm run build:valtown | bundle/backend.js + bundle/ui.js | Build completo para deploy |
backend/ index.ts widgets.ts frontend/ App.tsx main.tsx shared/ types.ts bundle/ backend.js ui.js widgets.json main.http.tsx
- Ejecutar
npm run build:valtown. - Subir a Val Town:
main.http.tsxbundle/backend.jsbundle/ui.jswidgets.json
- Usar
.vtignorepara excluir archivos no necesarios.
Widget schemas live under schemas/<widget>/vN.schema.json (JSON Schema Draft-07).
Checkbox: schemas/checkbox/v1.schema.json.
MIT