UNPKG

fortify-schema

Version:

A modern TypeScript validation library designed around familiar interface syntax and powerful conditional validation. Experience schema validation that feels natural to TypeScript developers while unlocking advanced runtime validation capabilities.

160 lines (157 loc) 5.38 kB
import { InterfaceSchema } from '../schema/mode/interfaces/InterfaceSchema.js'; /** * Schema modification utilities - transform and combine schemas */ class Mod { /** * Merge multiple schemas into a single schema * @example * ```typescript * const UserSchema = Interface({ id: "number", name: "string" }); * const ProfileSchema = Interface({ bio: "string?", avatar: "url?" }); * * const MergedSchema = Mod.merge(UserSchema, ProfileSchema); * // Result: { id: number, name: string, bio?: string, avatar?: string } * ``` */ static merge(schema1, schema2) { // Get the internal definitions from both schemas const def1 = schema1.definition; const def2 = schema2.definition; // Merge the definitions const mergedDefinition = { ...def1, ...def2 }; // Get options from both schemas (schema2 options take precedence) const options1 = schema1.options || {}; const options2 = schema2.options || {}; const mergedOptions = { ...options1, ...options2 }; return new InterfaceSchema(mergedDefinition, mergedOptions); } /** * Pick specific fields from a schema * @example * ```typescript * const UserSchema = Interface({ * id: "number", * name: "string", * email: "email", * password: "string" * }); * * const PublicUserSchema = Mod.pick(UserSchema, ["id", "name", "email"]); * // Result: { id: number, name: string, email: string } * ``` */ static pick(schema, keys) { const definition = schema.definition; const options = schema.options || {}; // Create new definition with only the picked keys const pickedDefinition = {}; for (const key of keys) { const keyStr = key; if (keyStr in definition) { pickedDefinition[keyStr] = definition[keyStr]; } } return new InterfaceSchema(pickedDefinition, options); } /** * Omit specific fields from a schema * @example * ```typescript * const UserSchema = Interface({ * id: "number", * name: "string", * email: "email", * password: "string" * }); * * const SafeUserSchema = Mod.omit(UserSchema, ["password"]); * // Result: { id: number, name: string, email: string } * ``` */ static omit(schema, keys) { const definition = schema.definition; const options = schema.options || {}; // Create new definition without the omitted keys const omittedDefinition = { ...definition }; for (const key of keys) { const keyStr = key; delete omittedDefinition[keyStr]; } return new InterfaceSchema(omittedDefinition, options); } /** * Make all fields in a schema optional * @example * ```typescript * const UserSchema = Interface({ id: "number", name: "string" }); * const PartialUserSchema = Mod.partial(UserSchema); * // Result: { id?: number, name?: string } * ``` */ static partial(schema) { const definition = schema.definition; const options = schema.options || {}; // Convert all fields to optional by adding "?" suffix const partialDefinition = {}; for (const [key, value] of Object.entries(definition)) { if (typeof value === "string" && !value.endsWith("?")) { partialDefinition[key] = value + "?"; } else { partialDefinition[key] = value; } } return new InterfaceSchema(partialDefinition, options); } /** * Make all fields in a schema required (remove optional markers) * @example * ```typescript * const UserSchema = Interface({ id: "number", name: "string?" }); * const RequiredUserSchema = Mod.required(UserSchema); * // Result: { id: number, name: string } * ``` */ static required(schema) { const definition = schema.definition; const options = schema.options || {}; // Remove "?" suffix from all fields const requiredDefinition = {}; for (const [key, value] of Object.entries(definition)) { if (typeof value === "string" && value.endsWith("?")) { requiredDefinition[key] = value.slice(0, -1); } else { requiredDefinition[key] = value; } } return new InterfaceSchema(requiredDefinition, options); } /** * Extend a schema with additional fields * @example * ```typescript * const BaseSchema = Interface({ id: "number", name: "string" }); * const ExtendedSchema = Mod.extend(BaseSchema, { * email: "email", * createdAt: "date" * }); * // Result: { id: number, name: string, email: string, createdAt: Date } * ``` */ static extend(schema, extension) { const definition = schema.definition; const options = schema.options || {}; const extendedDefinition = { ...definition, ...extension }; return new InterfaceSchema(extendedDefinition, options); } } export { Mod }; //# sourceMappingURL=Mod.js.map