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:
parent
316906aeb8
commit
7655eeb51c
5 changed files with 33 additions and 8 deletions
7
package-lock.json
generated
7
package-lock.json
generated
|
|
@ -19,6 +19,7 @@
|
|||
"express": "^5.0.1",
|
||||
"express-rate-limit": "^7.5.0",
|
||||
"jose": "^6.1.1",
|
||||
"json-schema-typed": "^8.0.2",
|
||||
"pkce-challenge": "^5.0.0",
|
||||
"raw-body": "^3.0.0",
|
||||
"zod": "^3.25 || ^4.0",
|
||||
|
|
@ -3172,6 +3173,12 @@
|
|||
"integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/json-schema-typed": {
|
||||
"version": "8.0.2",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-typed/-/json-schema-typed-8.0.2.tgz",
|
||||
"integrity": "sha512-fQhoXdcvc3V28x7C7BMs4P5+kNlgUURe2jmUT1T//oBRMDrqy1QPelJimwZGo7Hg9VPV3EQV5Bnq4hbFy2vetA==",
|
||||
"license": "BSD-2-Clause"
|
||||
},
|
||||
"node_modules/json-stable-stringify-without-jsonify": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz",
|
||||
|
|
|
|||
|
|
@ -96,6 +96,7 @@
|
|||
"express": "^5.0.1",
|
||||
"express-rate-limit": "^7.5.0",
|
||||
"jose": "^6.1.1",
|
||||
"json-schema-typed": "^8.0.2",
|
||||
"pkce-challenge": "^5.0.0",
|
||||
"raw-body": "^3.0.0",
|
||||
"zod": "^3.25 || ^4.0",
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue