From bec0f1da6d55a2d9b7a9dda99d392534d2cc30a3 Mon Sep 17 00:00:00 2001 From: jai <86279939+Jayavardhan11@users.noreply.github.com> Date: Wed, 23 Jul 2025 14:55:25 +0530 Subject: [PATCH] feat(core): add schema helper functions (string, number, object, etc.) Added core functions like string(), object(), array(), $ref(), nullable() to build JSON schemas concisely. --- src/schema-kit.ts | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/schema-kit.ts diff --git a/src/schema-kit.ts b/src/schema-kit.ts new file mode 100644 index 0000000..291c631 --- /dev/null +++ b/src/schema-kit.ts @@ -0,0 +1,78 @@ +import type { + Schema, + StringSchema, + NumberSchema, + IntegerSchema, + BooleanSchema, + ObjectSchema, + ArraySchema, + AnyOfSchema, + RefSchema, +} from './types' + +export function string(properties: Partial = {}): StringSchema { + return { + type: 'string', + ...properties, + } +} + +export function number(properties: Partial = {}): NumberSchema { + return { + type: 'number', + ...properties, + } +} + +export function integer(properties: Partial = {}): IntegerSchema { + return { + type: 'integer', + ...properties, + } +} + +export function boolean(properties: Partial = {}): BooleanSchema { + return { + type: 'boolean', + ...properties, + } +} + +export function object( + members: Record, + properties: Partial = {} +): ObjectSchema { + return { + type: 'object', + properties: members, + required: Object.keys(members), + additionalProperties: false, + ...properties, + } +} + +export function array(itemSchema: Schema, properties: Partial = {}): ArraySchema { + return { + type: 'array', + items: itemSchema, + ...properties, + } +} + +export function $ref(id: string): RefSchema { + return { + $ref: `#/$defs/${id}`, + } +} + +export function anyOf(subschemas: Schema[]): AnyOfSchema { + return { + anyOf: subschemas, + } +} + +export function nullable(schema: Schema): AnyOfSchema { + return 'anyOf' in schema + ? anyOf([...schema.anyOf, { type: 'null' }]) + : anyOf([schema, { type: 'null' }]) +}