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>
This commit is contained in:
Luca Chang 2025-12-05 09:13:45 -08:00 • committed by GitHub
commit 7655eeb51c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 8 deletions

View file

@ -93,14 +93,20 @@ function applyElicitationDefaults(schema: JsonSchemaType | undefined, data: unkn
if (Array.isArray(schema.anyOf)) {
for (const sub of schema.anyOf) {
applyElicitationDefaults(sub, data);
// Skip boolean schemas (true/false are valid JSON Schemas but have no defaults)
if (typeof sub !== 'boolean') {
applyElicitationDefaults(sub, data);
}
}
}
// Combine schemas
if (Array.isArray(schema.oneOf)) {
for (const sub of schema.oneOf) {
applyElicitationDefaults(sub, data);
// Skip boolean schemas (true/false are valid JSON Schemas but have no defaults)
if (typeof sub !== 'boolean') {
applyElicitationDefaults(sub, data);
}
}
}
}

View file

@ -6,7 +6,7 @@
* eval and new Function.
*/
import { type Schema, Validator } from '@cfworker/json-schema';
import { Validator } from '@cfworker/json-schema';
import type { JsonSchemaType, JsonSchemaValidator, JsonSchemaValidatorResult, jsonSchemaValidator } from './types.js';
/**
@ -53,8 +53,8 @@ export class CfWorkerJsonSchemaValidator implements jsonSchemaValidator {
* @returns A validator function that validates input data
*/
getValidator<T>(schema: JsonSchemaType): JsonSchemaValidator<T> {
const cfSchema = schema as unknown as Schema;
const validator = new Validator(cfSchema, this.draft, this.shortcircuit);
// Cast to the cfworker Schema type - our JsonSchemaType is structurally compatible
const validator = new Validator(schema as ConstructorParameters<typeof Validator>[0], this.draft, this.shortcircuit);
return (input: unknown): JsonSchemaValidatorResult<T> => {
const result = validator.validate(input);

View file

@ -1,4 +1,17 @@
import type { Schema } from '@cfworker/json-schema';
// Using the main export which points to draft-2020-12 by default
import type { JSONSchema } from 'json-schema-typed';
/**
* JSON Schema type definition (JSON Schema Draft 2020-12)
*
* This uses the object form of JSON Schema (excluding boolean schemas).
* While `true` and `false` are valid JSON Schemas, this SDK uses the
* object form for practical type safety.
*
* Re-exported from json-schema-typed for convenience.
* @see https://json-schema.org/draft/2020-12/json-schema-core.html
*/
export type JsonSchemaType = JSONSchema.Interface;
/**
* Result of a JSON Schema validation operation
@ -48,5 +61,3 @@ export interface jsonSchemaValidator {
*/
getValidator<T>(schema: JsonSchemaType): JsonSchemaValidator<T>;
}
export type JsonSchemaType = Schema;