UNPKG

fortify-schema

Version:

TypeScript interface-like schema validation system that's easier to use than Zod

183 lines (179 loc) 4.93 kB
'use strict'; var StringSchema = require('./StringSchema.js'); var NumberSchema = require('./NumberSchema.js'); var BooleanSchema = require('./BooleanSchema.js'); var ArraySchema = require('./ArraySchema.js'); var ObjectSchema = require('./ObjectSchema.js'); var BaseSchema = require('./BaseSchema.js'); /** * Main Schema factory - TypeScript-like schema validation * * Provides a clean, modular API for creating validation schemas without * mixing validation logic with schema definition logic. * * @example * ```typescript * import { Schema } from "fortify-schema"; * * // Define a user schema * const UserSchema = Schema.object({ * id: Schema.number().int().positive(), * email: Schema.string().email(), * name: Schema.string().min(2).max(50), * age: Schema.number().int().min(0).max(120).optional(), * isActive: Schema.boolean().default(true), * tags: Schema.array(Schema.string()).max(10).optional() * }); * * // Validate data * const result = UserSchema.safeParse({ * id: 1, * email: 'user@example.com', * name: 'John Doe', * isActive: true * }); * * if (result.success) { * console.log('Valid user:', result.data); * } else { * console.log('Validation errors:', result.errors); * } * ``` */ const Schema = { /** * Create a string schema * * @example * ```typescript * const nameSchema = Schema.string().min(2).max(50); * const emailSchema = Schema.string().email(); * const slugSchema = Schema.string().slug(); * ``` */ string() { return new StringSchema.StringSchema(); }, /** * Create a number schema * * @example * ```typescript * const ageSchema = Schema.number().int().min(0).max(120); * const priceSchema = Schema.number().positive().precision(2); * const idSchema = Schema.number().int().positive(); * ``` */ number() { return new NumberSchema.NumberSchema(); }, /** * Create a boolean schema * * @example * ```typescript * const activeSchema = Schema.boolean().default(true); * const strictBoolSchema = Schema.boolean().strict(); * ``` */ boolean() { return new BooleanSchema.BooleanSchema(); }, /** * Create an array schema * * @example * ```typescript * const tagsSchema = Schema.array(Schema.string()).min(1).max(10); * const numbersSchema = Schema.array(Schema.number()).unique(); * const usersSchema = Schema.array(UserSchema); * ``` */ array(elementSchema) { return new ArraySchema.ArraySchema(elementSchema); }, /** * Create an object schema * * @example * ```typescript * const userSchema = Schema.object({ * id: Schema.number().int(), * name: Schema.string(), * email: Schema.string().email() * }); * * const strictSchema = Schema.object({ * id: Schema.number() * }).strict(); // No extra properties * ``` */ object(shape) { return new ObjectSchema.ObjectSchema(shape); }, /** * Create a custom schema with validation function * * @example * ```typescript * const customSchema = Schema.custom<string>((value) => { * if (typeof value !== 'string') { * throw new Error('Expected string'); * } * if (!value.startsWith('custom_')) { * throw new Error('Must start with custom_'); * } * return value; * }); * ``` */ custom(validator) { return new CustomSchema(validator); }, }; /** * Custom schema implementation */ class CustomSchema extends BaseSchema.BaseSchema { constructor(validator) { super(); this.validator = validator; } validate(value) { // Handle common validation (undefined/null) const commonResult = this.handleCommonValidation(value); if (commonResult) { return commonResult; } const result = { success: true, errors: [], warnings: [], data: undefined, }; try { result.data = this.validator(value); } catch (error) { result.success = false; result.errors.push(error.message); } return result; } getConfig() { return { type: "custom", optional: this._optional, nullable: this._nullable, default: this._default, }; } } exports.StringSchema = StringSchema.StringSchema; exports.NumberSchema = NumberSchema.NumberSchema; exports.BooleanSchema = BooleanSchema.BooleanSchema; exports.ArraySchema = ArraySchema.ArraySchema; exports.ObjectSchema = ObjectSchema.ObjectSchema; exports.BaseSchema = BaseSchema.BaseSchema; exports.Schema = Schema; //# sourceMappingURL=Schema.js.map