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.

69 lines (66 loc) 1.88 kB
import { ConditionalThen } from './ConditionalThen.js'; /** * Builder for single field conditional validation with TypeScript inference */ class ConditionalBuilder { constructor(fieldName) { this.fieldName = fieldName; this.conditions = []; this.defaultSchema = null; } /** * Check if field equals specific value */ is(value) { return new ConditionalThen(this, (fieldValue) => fieldValue === value); } /** * Check if field does not equal specific value */ isNot(value) { return new ConditionalThen(this, (fieldValue) => fieldValue !== value); } /** * Check if field exists (not null/undefined) */ exists() { return new ConditionalThen(this, (fieldValue) => fieldValue != null); } /** * Check if field matches pattern */ matches(pattern) { return new ConditionalThen(this, (fieldValue) => typeof fieldValue === "string" && pattern.test(fieldValue)); } /** * Check if field is in array of values */ in(values) { return new ConditionalThen(this, (fieldValue) => values.includes(fieldValue)); } /** * Custom condition function */ when(condition) { return new ConditionalThen(this, condition); } addCondition(condition, schema) { this.conditions.push({ condition, schema }); return this; } default(schema) { this.defaultSchema = schema; return this.build(); } build() { // Return a special object that the validation engine can recognize return { __conditional: true, fieldName: this.fieldName, conditions: this.conditions, default: this.defaultSchema }; } } export { ConditionalBuilder }; //# sourceMappingURL=ConditionalBuilder.js.map