fortify-schema
Version:
TypeScript interface-like schema validation system that's easier to use than Zod
39 lines (37 loc) • 1.22 kB
JavaScript
/**
* Builder for the "else" part of conditional validation with TypeScript inference
* This class is returned after calling .then() and provides the .else() method
*/
class ConditionalElse {
constructor(builder, condition, thenSchema) {
this.builder = builder;
this.condition = condition;
this.thenSchema = thenSchema;
}
/**
* Specify the schema to use when the condition is false
*/
else(elseSchema) {
// Add the condition with the "then" schema
this.builder.addCondition(this.condition, this.thenSchema);
// Set the default schema (used when condition is false)
const result = this.builder.default(elseSchema);
// Return with type information for TypeScript inference
return result;
}
/**
* Alias for else() - for backward compatibility
*/
default(defaultSchema) {
return this.else(defaultSchema);
}
/**
* Build without else clause (same as calling .else(undefined))
*/
build() {
this.builder.addCondition(this.condition, this.thenSchema);
return this.builder.build();
}
}
export { ConditionalElse };
//# sourceMappingURL=ConditionalElse.js.map