@titanpl/valid
v1.1.1
Published
titan-valid is a robust validation library built specifically for the Titan ecosystem. It provides a simple, fluent API for validating complex data structures with meaningful error messages. It is pure JavaScript, fast, and dependency-free.
Maintainers
Readme
🪐 Titan Extension: titan-valid
A lightweight, chainable, production-ready validation engine for Titan Planet.
titan-valid is a robust validation library built specifically for the Titan ecosystem. It provides a simple, fluent API for validating complex data structures with meaningful error messages. It is pure JavaScript, fast, and dependency-free.
� Features
- Chainable API: Easy to read and write rules (e.g.,
.min(5).max(10).email()). - Comprehensive Types: Support for String, Number, Boolean, Object, and Array.
- Nested Validation: Deep validation for objects and arrays with dot-notation error paths.
- Zero Dependencies: Lightweight and extremely fast.
- Titan Native integration: Automatically exposes itself as
t.valid.
� Installation
This extension comes pre-packaged or can be installed via npm in your Titan project:
npm install titan-valid💻 Usage
Once installed, the library is available globally as t.valid within the Titan Runtime.
Basic Examples
String Validation
const schema = t.valid.string()
.min(3)
.max(20)
.email();
try {
schema.parse("[email protected]"); // returns "[email protected]"
} catch (error) {
console.log(error.errors);
// [{ field: "root", rule: "email", message: "Must be a valid email" }]
}Object Validation
const userSchema = t.valid.object({
username: t.valid.string().min(3),
age: t.valid.number().min(18),
isAdmin: t.valid.boolean(),
tags: t.valid.array(t.valid.string()).min(1)
});
try {
userSchema.parse({
username: "TitanUser",
age: 25,
isAdmin: false,
tags: ["developer", "extension"]
});
} catch (e) {
console.error(e.message);
}export const register = (req) => {
// 1. Define Validation Schema
const schema = t.valid.object({
username: t.valid.string().min(3).max(30),
email: t.valid.string().email(),
password: t.valid.string().min(6)
});
try {
const body = req.body || {};
// 2. Validate
schema.parse(body);
let hashedPassword = t.password.hash(body.password);
// t.log(hashedPassword)
t.log("Register", `New user registered: ${body.username}`);
return {
success: true,
message: "User created successfully",
user: {
username: body.username,
email: body.email
}
};
} catch (error) {
if (error.name === "ValidationError") {
// t.log(error)
return { error: error.message };
} else {
t.log("Register Error", error.message);
return { error: "Internal Server Error" };
}
}
}Supported API
t.valid.string()
.min(n): Minimum length..max(n): Maximum length..length(n): Exact length..email(): Validates email format..regex(pattern): Matches a RegExp pattern.
t.valid.number()
.min(n): Minimum value..max(n): Maximum value.
t.valid.array(type)
.min(n): Minimum number of items..max(n): Maximum number of items..length(n): Exact number of items.
t.valid.object(shape)
- Validates object structure recursively.
t.valid.optional(type)
- Allows
nullorundefinedvalues.
� Project Structure
index.js: Main library source code.titan.json: Extension manifest.
🧪 Testing
You can test changes to this extension using the Titan SDK runner:
titan run extVisit http://localhost:3000/test to see the validation engine in action.
Happy coding on Titan Planet! 🚀
