@formspec/validator 0.1.0-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # @formspec/validator
2
+
3
+ JSON Schema validation for FormSpec, powered by [@cfworker/json-schema](https://github.com/nicolo-ribaudo/cfworker-json-schema). Designed for secure runtime environments that prohibit `new Function()` and `eval()`.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @formspec/validator
9
+ # or
10
+ pnpm add @formspec/validator
11
+ ```
12
+
13
+ ## Requirements
14
+
15
+ This package is ESM-only and requires:
16
+
17
+ ```json
18
+ // package.json
19
+ {
20
+ "type": "module"
21
+ }
22
+ ```
23
+
24
+ ```json
25
+ // tsconfig.json
26
+ {
27
+ "compilerOptions": {
28
+ "module": "NodeNext",
29
+ "moduleResolution": "NodeNext"
30
+ }
31
+ }
32
+ ```
33
+
34
+ ## Quick Start
35
+
36
+ ```typescript
37
+ import { createFormSpecValidator } from "@formspec/validator";
38
+ import { buildFormSchemas, formspec, field } from "formspec";
39
+
40
+ const form = formspec(
41
+ field.text("name", { required: true }),
42
+ field.number("age", { min: 0 })
43
+ );
44
+
45
+ const { jsonSchema } = buildFormSchemas(form);
46
+
47
+ // Create a validator that ignores x-formspec-* extension keywords
48
+ const validator = createFormSpecValidator(jsonSchema);
49
+
50
+ const result = validator.validate({ name: "Alice", age: 30 });
51
+ console.log(result.valid); // true
52
+ ```
53
+
54
+ ## Why This Package?
55
+
56
+ Standard JSON Schema validators like Ajv use `new Function()` internally, which is blocked in:
57
+
58
+ - Cloudflare Workers
59
+ - Deno Deploy
60
+ - Browser extensions (CSP restrictions)
61
+ - Any environment with strict Content Security Policy
62
+
63
+ `@formspec/validator` wraps `@cfworker/json-schema`, which implements JSON Schema validation without code generation. It also pre-configures the validator to silently ignore `x-formspec-*` vendor extension keywords that FormSpec adds to generated schemas.
64
+
65
+ ## API Reference
66
+
67
+ ### Functions
68
+
69
+ | Function | Description |
70
+ | --- | --- |
71
+ | `createFormSpecValidator(schema, options?)` | Create a validator instance for a JSON Schema |
72
+
73
+ ### Options
74
+
75
+ ```typescript
76
+ interface CreateValidatorOptions {
77
+ draft?: SchemaDraft; // JSON Schema draft version (default: "2020-12")
78
+ shortCircuit?: boolean; // Stop on first error (default: true)
79
+ }
80
+ ```
81
+
82
+ ### Re-exports
83
+
84
+ This package re-exports key types from `@cfworker/json-schema`:
85
+
86
+ | Export | Description |
87
+ | --- | --- |
88
+ | `Validator` | The validator class |
89
+ | `ValidationResult` | Result of `validator.validate()` |
90
+ | `OutputUnit` | Individual validation error detail |
91
+ | `SchemaDraft` | Supported JSON Schema draft versions |
92
+
93
+ ## License
94
+
95
+ UNLICENSED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=validator.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validator.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/validator.test.ts"],"names":[],"mappings":""}
package/dist/index.cjs ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Validator: () => import_json_schema.Validator,
24
+ createFormSpecValidator: () => createFormSpecValidator
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_json_schema = require("@cfworker/json-schema");
28
+ function createFormSpecValidator(schema, options) {
29
+ const draft = options?.draft ?? "2020-12";
30
+ const shortCircuit = options?.shortCircuit ?? true;
31
+ return new import_json_schema.Validator(schema, draft, shortCircuit);
32
+ }
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ Validator,
36
+ createFormSpecValidator
37
+ });
38
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * `@formspec/validator` — JSON Schema validator for FormSpec\n *\n * Backed by {@link https://github.com/cfworker/cfworker/tree/main/packages/json-schema | @cfworker/json-schema},\n * this package validates data against FormSpec-generated JSON Schemas in any\n * JavaScript runtime — including secure environments that disallow `new Function()`\n * (e.g., Cloudflare Workers).\n *\n * Unlike Ajv, `@cfworker/json-schema` silently ignores unknown keywords, so\n * FormSpec's `x-formspec-*` extension keywords require no vocabulary registration.\n *\n * @example\n * ```typescript\n * import { createFormSpecValidator } from \"@formspec/validator\";\n *\n * const validator = createFormSpecValidator({\n * type: \"object\",\n * required: [\"name\"],\n * properties: {\n * name: { type: \"string\" },\n * country: { type: \"string\", \"x-formspec-source\": \"countries\" },\n * },\n * });\n *\n * const result = validator.validate({ name: \"Alice\", country: \"US\" });\n * // result.valid === true\n * ```\n *\n * @packageDocumentation\n */\n\nimport { Validator } from \"@cfworker/json-schema\";\nimport type { OutputUnit, Schema, SchemaDraft, ValidationResult } from \"@cfworker/json-schema\";\n\nexport { Validator };\nexport type { OutputUnit, SchemaDraft, ValidationResult };\n\n/**\n * Options for `createFormSpecValidator`.\n *\n * @public\n */\nexport interface CreateValidatorOptions {\n /**\n * The JSON Schema draft version to validate against.\n *\n * FormSpec generates schemas targeting JSON Schema 2020-12.\n *\n * @defaultValue `\"2020-12\"`\n */\n draft?: SchemaDraft;\n\n /**\n * When `true`, validation stops after the first error.\n * When `false`, all errors are collected.\n *\n * @defaultValue `true`\n */\n shortCircuit?: boolean;\n}\n\n/**\n * Creates a `Validator` configured for FormSpec-generated JSON Schemas.\n *\n * The returned `Validator` uses JSON Schema draft 2020-12 by default and\n * silently ignores `x-formspec-*` extension keywords — no vocabulary\n * registration is needed.\n *\n * @param schema - A JSON Schema object (typically generated by `@formspec/build`).\n * @param options - Optional configuration for draft version and error collection behavior.\n * @returns A `Validator` instance whose `validate()` method returns a `ValidationResult`.\n *\n * @public\n */\nexport function createFormSpecValidator(\n schema: Record<string, unknown>,\n options?: CreateValidatorOptions,\n): Validator {\n const draft = options?.draft ?? \"2020-12\";\n const shortCircuit = options?.shortCircuit ?? true;\n // Safe cast: Schema has `[key: string]: any`, so every Record<string, unknown>\n // structurally satisfies it. We keep the public parameter as Record<string, unknown>\n // to avoid coupling consumers to @cfworker/json-schema's Schema type.\n return new Validator(schema as Schema, draft, shortCircuit);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+BA,yBAA0B;AA2CnB,SAAS,wBACd,QACA,SACW;AACX,QAAM,QAAQ,SAAS,SAAS;AAChC,QAAM,eAAe,SAAS,gBAAgB;AAI9C,SAAO,IAAI,6BAAU,QAAkB,OAAO,YAAY;AAC5D;","names":[]}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * `@formspec/validator` — JSON Schema validator for FormSpec
3
+ *
4
+ * Backed by {@link https://github.com/cfworker/cfworker/tree/main/packages/json-schema | @cfworker/json-schema},
5
+ * this package validates data against FormSpec-generated JSON Schemas in any
6
+ * JavaScript runtime — including secure environments that disallow `new Function()`
7
+ * (e.g., Cloudflare Workers).
8
+ *
9
+ * Unlike Ajv, `@cfworker/json-schema` silently ignores unknown keywords, so
10
+ * FormSpec's `x-formspec-*` extension keywords require no vocabulary registration.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { createFormSpecValidator } from "@formspec/validator";
15
+ *
16
+ * const validator = createFormSpecValidator({
17
+ * type: "object",
18
+ * required: ["name"],
19
+ * properties: {
20
+ * name: { type: "string" },
21
+ * country: { type: "string", "x-formspec-source": "countries" },
22
+ * },
23
+ * });
24
+ *
25
+ * const result = validator.validate({ name: "Alice", country: "US" });
26
+ * // result.valid === true
27
+ * ```
28
+ *
29
+ * @packageDocumentation
30
+ */
31
+ import { Validator } from "@cfworker/json-schema";
32
+ import type { OutputUnit, SchemaDraft, ValidationResult } from "@cfworker/json-schema";
33
+ export { Validator };
34
+ export type { OutputUnit, SchemaDraft, ValidationResult };
35
+ /**
36
+ * Options for `createFormSpecValidator`.
37
+ *
38
+ * @public
39
+ */
40
+ export interface CreateValidatorOptions {
41
+ /**
42
+ * The JSON Schema draft version to validate against.
43
+ *
44
+ * FormSpec generates schemas targeting JSON Schema 2020-12.
45
+ *
46
+ * @defaultValue `"2020-12"`
47
+ */
48
+ draft?: SchemaDraft;
49
+ /**
50
+ * When `true`, validation stops after the first error.
51
+ * When `false`, all errors are collected.
52
+ *
53
+ * @defaultValue `true`
54
+ */
55
+ shortCircuit?: boolean;
56
+ }
57
+ /**
58
+ * Creates a `Validator` configured for FormSpec-generated JSON Schemas.
59
+ *
60
+ * The returned `Validator` uses JSON Schema draft 2020-12 by default and
61
+ * silently ignores `x-formspec-*` extension keywords — no vocabulary
62
+ * registration is needed.
63
+ *
64
+ * @param schema - A JSON Schema object (typically generated by `@formspec/build`).
65
+ * @param options - Optional configuration for draft version and error collection behavior.
66
+ * @returns A `Validator` instance whose `validate()` method returns a `ValidationResult`.
67
+ *
68
+ * @public
69
+ */
70
+ export declare function createFormSpecValidator(schema: Record<string, unknown>, options?: CreateValidatorOptions): Validator;
71
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,KAAK,EAAE,UAAU,EAAU,WAAW,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE/F,OAAO,EAAE,SAAS,EAAE,CAAC;AACrB,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,CAAC;AAE1D;;;;GAIG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,WAAW,CAAC;IAEpB;;;;;OAKG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,OAAO,CAAC,EAAE,sBAAsB,GAC/B,SAAS,CAOX"}
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ // src/index.ts
2
+ import { Validator } from "@cfworker/json-schema";
3
+ function createFormSpecValidator(schema, options) {
4
+ const draft = options?.draft ?? "2020-12";
5
+ const shortCircuit = options?.shortCircuit ?? true;
6
+ return new Validator(schema, draft, shortCircuit);
7
+ }
8
+ export {
9
+ Validator,
10
+ createFormSpecValidator
11
+ };
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * `@formspec/validator` — JSON Schema validator for FormSpec\n *\n * Backed by {@link https://github.com/cfworker/cfworker/tree/main/packages/json-schema | @cfworker/json-schema},\n * this package validates data against FormSpec-generated JSON Schemas in any\n * JavaScript runtime — including secure environments that disallow `new Function()`\n * (e.g., Cloudflare Workers).\n *\n * Unlike Ajv, `@cfworker/json-schema` silently ignores unknown keywords, so\n * FormSpec's `x-formspec-*` extension keywords require no vocabulary registration.\n *\n * @example\n * ```typescript\n * import { createFormSpecValidator } from \"@formspec/validator\";\n *\n * const validator = createFormSpecValidator({\n * type: \"object\",\n * required: [\"name\"],\n * properties: {\n * name: { type: \"string\" },\n * country: { type: \"string\", \"x-formspec-source\": \"countries\" },\n * },\n * });\n *\n * const result = validator.validate({ name: \"Alice\", country: \"US\" });\n * // result.valid === true\n * ```\n *\n * @packageDocumentation\n */\n\nimport { Validator } from \"@cfworker/json-schema\";\nimport type { OutputUnit, Schema, SchemaDraft, ValidationResult } from \"@cfworker/json-schema\";\n\nexport { Validator };\nexport type { OutputUnit, SchemaDraft, ValidationResult };\n\n/**\n * Options for `createFormSpecValidator`.\n *\n * @public\n */\nexport interface CreateValidatorOptions {\n /**\n * The JSON Schema draft version to validate against.\n *\n * FormSpec generates schemas targeting JSON Schema 2020-12.\n *\n * @defaultValue `\"2020-12\"`\n */\n draft?: SchemaDraft;\n\n /**\n * When `true`, validation stops after the first error.\n * When `false`, all errors are collected.\n *\n * @defaultValue `true`\n */\n shortCircuit?: boolean;\n}\n\n/**\n * Creates a `Validator` configured for FormSpec-generated JSON Schemas.\n *\n * The returned `Validator` uses JSON Schema draft 2020-12 by default and\n * silently ignores `x-formspec-*` extension keywords — no vocabulary\n * registration is needed.\n *\n * @param schema - A JSON Schema object (typically generated by `@formspec/build`).\n * @param options - Optional configuration for draft version and error collection behavior.\n * @returns A `Validator` instance whose `validate()` method returns a `ValidationResult`.\n *\n * @public\n */\nexport function createFormSpecValidator(\n schema: Record<string, unknown>,\n options?: CreateValidatorOptions,\n): Validator {\n const draft = options?.draft ?? \"2020-12\";\n const shortCircuit = options?.shortCircuit ?? true;\n // Safe cast: Schema has `[key: string]: any`, so every Record<string, unknown>\n // structurally satisfies it. We keep the public parameter as Record<string, unknown>\n // to avoid coupling consumers to @cfworker/json-schema's Schema type.\n return new Validator(schema as Schema, draft, shortCircuit);\n}\n"],"mappings":";AA+BA,SAAS,iBAAiB;AA2CnB,SAAS,wBACd,QACA,SACW;AACX,QAAM,QAAQ,SAAS,SAAS;AAChC,QAAM,eAAe,SAAS,gBAAgB;AAI9C,SAAO,IAAI,UAAU,QAAkB,OAAO,YAAY;AAC5D;","names":[]}
@@ -0,0 +1,83 @@
1
+ /**
2
+ * `@formspec/validator` — JSON Schema validator for FormSpec
3
+ *
4
+ * Backed by {@link https://github.com/cfworker/cfworker/tree/main/packages/json-schema | @cfworker/json-schema},
5
+ * this package validates data against FormSpec-generated JSON Schemas in any
6
+ * JavaScript runtime — including secure environments that disallow `new Function()`
7
+ * (e.g., Cloudflare Workers).
8
+ *
9
+ * Unlike Ajv, `@cfworker/json-schema` silently ignores unknown keywords, so
10
+ * FormSpec's `x-formspec-*` extension keywords require no vocabulary registration.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { createFormSpecValidator } from "@formspec/validator";
15
+ *
16
+ * const validator = createFormSpecValidator({
17
+ * type: "object",
18
+ * required: ["name"],
19
+ * properties: {
20
+ * name: { type: "string" },
21
+ * country: { type: "string", "x-formspec-source": "countries" },
22
+ * },
23
+ * });
24
+ *
25
+ * const result = validator.validate({ name: "Alice", country: "US" });
26
+ * // result.valid === true
27
+ * ```
28
+ *
29
+ * @packageDocumentation
30
+ */
31
+
32
+ import type { OutputUnit } from '@cfworker/json-schema';
33
+ import type { SchemaDraft } from '@cfworker/json-schema';
34
+ import type { ValidationResult } from '@cfworker/json-schema';
35
+ import { Validator } from '@cfworker/json-schema';
36
+
37
+ /**
38
+ * Creates a `Validator` configured for FormSpec-generated JSON Schemas.
39
+ *
40
+ * The returned `Validator` uses JSON Schema draft 2020-12 by default and
41
+ * silently ignores `x-formspec-*` extension keywords — no vocabulary
42
+ * registration is needed.
43
+ *
44
+ * @param schema - A JSON Schema object (typically generated by `@formspec/build`).
45
+ * @param options - Optional configuration for draft version and error collection behavior.
46
+ * @returns A `Validator` instance whose `validate()` method returns a `ValidationResult`.
47
+ *
48
+ * @public
49
+ */
50
+ export declare function createFormSpecValidator(schema: Record<string, unknown>, options?: CreateValidatorOptions): Validator;
51
+
52
+ /**
53
+ * Options for `createFormSpecValidator`.
54
+ *
55
+ * @public
56
+ */
57
+ export declare interface CreateValidatorOptions {
58
+ /**
59
+ * The JSON Schema draft version to validate against.
60
+ *
61
+ * FormSpec generates schemas targeting JSON Schema 2020-12.
62
+ *
63
+ * @defaultValue `"2020-12"`
64
+ */
65
+ draft?: SchemaDraft;
66
+ /**
67
+ * When `true`, validation stops after the first error.
68
+ * When `false`, all errors are collected.
69
+ *
70
+ * @defaultValue `true`
71
+ */
72
+ shortCircuit?: boolean;
73
+ }
74
+
75
+ export { OutputUnit }
76
+
77
+ export { SchemaDraft }
78
+
79
+ export { ValidationResult }
80
+
81
+ export { Validator }
82
+
83
+ export { }
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@formspec/validator",
3
+ "version": "0.1.0-alpha.12",
4
+ "description": "JSON Schema validator for FormSpec — backed by @cfworker/json-schema, safe for secure runtimes",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/validator.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/validator.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "dependencies": {
21
+ "@cfworker/json-schema": "^4.1.0"
22
+ },
23
+ "devDependencies": {
24
+ "vitest": "^3.0.0"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "keywords": [
30
+ "formspec",
31
+ "validator",
32
+ "jsonschema",
33
+ "cloudflare-workers"
34
+ ],
35
+ "license": "UNLICENSED",
36
+ "scripts": {
37
+ "build": "tsup && tsc --emitDeclarationOnly && api-extractor run --local",
38
+ "clean": "rm -rf dist temp",
39
+ "typecheck": "tsc --noEmit",
40
+ "test": "vitest run",
41
+ "api-extractor": "api-extractor run",
42
+ "api-extractor:local": "api-extractor run --local",
43
+ "api-documenter": "api-documenter markdown -i temp -o docs"
44
+ }
45
+ }