Skip to content

Commit 7655eeb

Browse files
Remove type dependency on @cfworker/json-schema (#1229)
Co-authored-by: Matt Carey <mcarey@cloudflare.com> Co-authored-by: Matt <77928207+mattzcarey@users.noreply.github.com>
1 parent 316906a commit 7655eeb

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"express": "^5.0.1",
9797
"express-rate-limit": "^7.5.0",
9898
"jose": "^6.1.1",
99+
"json-schema-typed": "^8.0.2",
99100
"pkce-challenge": "^5.0.0",
100101
"raw-body": "^3.0.0",
101102
"zod": "^3.25 || ^4.0",

src/client/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,20 @@ function applyElicitationDefaults(schema: JsonSchemaType | undefined, data: unkn
9393

9494
if (Array.isArray(schema.anyOf)) {
9595
for (const sub of schema.anyOf) {
96-
applyElicitationDefaults(sub, data);
96+
// Skip boolean schemas (true/false are valid JSON Schemas but have no defaults)
97+
if (typeof sub !== 'boolean') {
98+
applyElicitationDefaults(sub, data);
99+
}
97100
}
98101
}
99102

100103
// Combine schemas
101104
if (Array.isArray(schema.oneOf)) {
102105
for (const sub of schema.oneOf) {
103-
applyElicitationDefaults(sub, data);
106+
// Skip boolean schemas (true/false are valid JSON Schemas but have no defaults)
107+
if (typeof sub !== 'boolean') {
108+
applyElicitationDefaults(sub, data);
109+
}
104110
}
105111
}
106112
}

src/validation/cfworker-provider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* eval and new Function.
77
*/
88

9-
import { type Schema, Validator } from '@cfworker/json-schema';
9+
import { Validator } from '@cfworker/json-schema';
1010
import type { JsonSchemaType, JsonSchemaValidator, JsonSchemaValidatorResult, jsonSchemaValidator } from './types.js';
1111

1212
/**
@@ -53,8 +53,8 @@ export class CfWorkerJsonSchemaValidator implements jsonSchemaValidator {
5353
* @returns A validator function that validates input data
5454
*/
5555
getValidator<T>(schema: JsonSchemaType): JsonSchemaValidator<T> {
56-
const cfSchema = schema as unknown as Schema;
57-
const validator = new Validator(cfSchema, this.draft, this.shortcircuit);
56+
// Cast to the cfworker Schema type - our JsonSchemaType is structurally compatible
57+
const validator = new Validator(schema as ConstructorParameters<typeof Validator>[0], this.draft, this.shortcircuit);
5858

5959
return (input: unknown): JsonSchemaValidatorResult<T> => {
6060
const result = validator.validate(input);

src/validation/types.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
import type { Schema } from '@cfworker/json-schema';
1+
// Using the main export which points to draft-2020-12 by default
2+
import type { JSONSchema } from 'json-schema-typed';
3+
4+
/**
5+
* JSON Schema type definition (JSON Schema Draft 2020-12)
6+
*
7+
* This uses the object form of JSON Schema (excluding boolean schemas).
8+
* While `true` and `false` are valid JSON Schemas, this SDK uses the
9+
* object form for practical type safety.
10+
*
11+
* Re-exported from json-schema-typed for convenience.
12+
* @see https://json-schema.org/draft/2020-12/json-schema-core.html
13+
*/
14+
export type JsonSchemaType = JSONSchema.Interface;
215

316
/**
417
* Result of a JSON Schema validation operation
@@ -48,5 +61,3 @@ export interface jsonSchemaValidator {
4861
*/
4962
getValidator<T>(schema: JsonSchemaType): JsonSchemaValidator<T>;
5063
}
51-
52-
export type JsonSchemaType = Schema;

0 commit comments

Comments
 (0)