fortify-schema
Version:
TypeScript interface-like schema validation system that's easier to use than Zod
175 lines (172 loc) • 4.62 kB
JavaScript
import { StringSchema } from './StringSchema.js';
import { NumberSchema } from './NumberSchema.js';
import { BooleanSchema } from './BooleanSchema.js';
import { ArraySchema } from './ArraySchema.js';
import { ObjectSchema } from './ObjectSchema.js';
import { BaseSchema } from './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();
},
/**
* 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();
},
/**
* Create a boolean schema
*
* @example
* ```typescript
* const activeSchema = Schema.boolean().default(true);
* const strictBoolSchema = Schema.boolean().strict();
* ```
*/
boolean() {
return new 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(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(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 {
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,
};
}
}
export { ArraySchema, BaseSchema, BooleanSchema, NumberSchema, ObjectSchema, Schema, StringSchema };
//# sourceMappingURL=Schema.js.map