@formspec/validator 0.1.0-alpha.12 → 0.1.0-alpha.26
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 +22 -75
- package/dist/index.cjs.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/validator-alpha.d.ts +83 -0
- package/dist/validator-beta.d.ts +83 -0
- package/dist/validator-internal.d.ts +83 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,94 +1,41 @@
|
|
|
1
1
|
# @formspec/validator
|
|
2
2
|
|
|
3
|
-
JSON Schema validation for FormSpec,
|
|
3
|
+
Runtime JSON Schema validation for FormSpec, backed by `@cfworker/json-schema`.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This package is intended for environments where code-generating validators are a bad fit, including workers and CSP-restricted runtimes.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
6
8
|
|
|
7
9
|
```bash
|
|
8
|
-
npm install @formspec/validator
|
|
9
|
-
# or
|
|
10
10
|
pnpm add @formspec/validator
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
##
|
|
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
|
|
13
|
+
## Usage
|
|
35
14
|
|
|
36
|
-
```
|
|
15
|
+
```ts
|
|
37
16
|
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
17
|
|
|
73
|
-
|
|
18
|
+
const validator = createFormSpecValidator({
|
|
19
|
+
type: "object",
|
|
20
|
+
required: ["name"],
|
|
21
|
+
properties: {
|
|
22
|
+
name: { type: "string" },
|
|
23
|
+
country: { type: "string", "x-formspec-source": "countries" },
|
|
24
|
+
},
|
|
25
|
+
});
|
|
74
26
|
|
|
75
|
-
|
|
76
|
-
interface CreateValidatorOptions {
|
|
77
|
-
draft?: SchemaDraft; // JSON Schema draft version (default: "2020-12")
|
|
78
|
-
shortCircuit?: boolean; // Stop on first error (default: true)
|
|
79
|
-
}
|
|
27
|
+
const result = validator.validate({ name: "Alice", country: "us" });
|
|
80
28
|
```
|
|
81
29
|
|
|
82
|
-
|
|
30
|
+
Unknown `x-formspec-*` keywords are ignored by the underlying validator, so generated schemas work without extra vocabulary registration.
|
|
83
31
|
|
|
84
|
-
|
|
32
|
+
## Main Exports
|
|
85
33
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
| `SchemaDraft` | Supported JSON Schema draft versions |
|
|
34
|
+
- `createFormSpecValidator(schema, options?)`
|
|
35
|
+
- `Validator`
|
|
36
|
+
- `ValidationResult`
|
|
37
|
+
- `OutputUnit`
|
|
38
|
+
- `SchemaDraft`
|
|
92
39
|
|
|
93
40
|
## License
|
|
94
41
|
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +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
|
|
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":[]}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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
|
|
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 { }
|
|
@@ -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 { }
|
|
@@ -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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formspec/validator",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.26",
|
|
4
4
|
"description": "JSON Schema validator for FormSpec — backed by @cfworker/json-schema, safe for secure runtimes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -34,12 +34,12 @@
|
|
|
34
34
|
],
|
|
35
35
|
"license": "UNLICENSED",
|
|
36
36
|
"scripts": {
|
|
37
|
-
"build": "tsup && tsc --emitDeclarationOnly && api-extractor
|
|
37
|
+
"build": "tsup && tsc --emitDeclarationOnly && pnpm run api-extractor:local",
|
|
38
38
|
"clean": "rm -rf dist temp",
|
|
39
39
|
"typecheck": "tsc --noEmit",
|
|
40
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"
|
|
41
|
+
"api-extractor": "api-extractor run && node ../../scripts/normalize-generated-markdown.mjs api-report",
|
|
42
|
+
"api-extractor:local": "api-extractor run --local && node ../../scripts/normalize-generated-markdown.mjs api-report",
|
|
43
|
+
"api-documenter": "api-documenter markdown -i temp -o docs && node ../../scripts/normalize-generated-markdown.mjs docs"
|
|
44
44
|
}
|
|
45
45
|
}
|