typix
v1.0.18
Published
A TypeScript library for validating object typings and values with custom rules, offering both strict type checking and value validation.
Downloads
122
Maintainers
Readme
Typix 🧹
A TypeScript library for validating object typings and values with custom rules, offering both strict type checking and value validation.
🔗 Repository
Installation
Install via npm:
npm install typixOr using Yarn:
yarn add typixHow It Works
Type and Value Validation
Typix helps validate objects by checking their field types and values:
- Ensures the fields match the expected types.
- Allows custom value validation rules for each field (e.g., ensuring
IDis not-1). - Supports strict validation mode, where all fields must be present and match their types unless specified otherwise.
- Individual fields can have a
strictproperty set tofalse, making them optional and skipping validation if the field is missing orundefined.
Usage
1. Validating an Object
import typix from "typix";
const options = {
fields: [
{ name: "ID", type: "number", validateValue: (value) => value !== -1 }, // Custom rule for ID
{ name: "NAME", type: "string" },
{ name: "AGE", type: "number" },
],
strict: true,
};
const testData = {
ID: 1,
NAME: "John Doe",
AGE: 30,
};
const result = await typix.validate(options, testData);
console.log(result.isValid); // true
console.log(result.message); // "Field validations were successful"What Happens?
Typixchecks if theIDis anumberand not equal to-1.- Validates
NAMEas astringandAGEas anumber. - Returns a result with validation success or failure.
2. Handling Validation Failures
import typix from "typix";
const options = {
fields: [
{ name: "ID", type: "number", validateValue: (value) => value !== -1 },
{ name: "NAME", type: "string" },
{ name: "AGE", type: "number" },
],
strict: true,
};
const testData = {
ID: "1", // Invalid type for ID
NAME: "John Doe",
AGE: 30,
};
const result = await typix.validate(options, testData);
console.log(result.isValid); // false
console.log(result.message); // "One or more field validations failed"
console.log(result.expectedFields); // List of validation errorsWhat Happens?
- If the
IDis of the wrong type (e.g., a string instead of a number), the validation will fail. - A detailed message with the
expectedType,receivedValue, and error type will be provided.
3. Using Field-Level Strictness
import typix from "typix";
const options = {
fields: [
{ name: "ID", type: "number" },
{ name: "NAME", type: "string", strict: false }, // NAME is optional
{ name: "AGE", type: "number" },
],
strict: true,
};
const testData = {
ID: 1,
NAME: undefined, // Skipped due to strict: false
AGE: 24,
};
const result = await typix.validate(options, testData);
console.log(result.isValid); // true
console.log(result.message); // "Field validations were successful"What Happens?
- The
NAMEfield is optional because itsstrictproperty isfalse, soundefinedis allowed. - Other fields (
IDandAGE) are still validated as mandatory due to the globalstrict: true.
4. Non-Strict Global Validation
import typix from "typix";
const options = {
fields: [
{ name: "ID", type: "number" },
{ name: "NAME", type: "string", strict: false },
{ name: "AGE", type: "number" },
],
strict: false, // No fields are mandatory
};
const testData = {
ID: undefined,
NAME: undefined,
AGE: undefined,
};
const result = await typix.validate(options, testData);
console.log(result.isValid); // true
console.log(result.message); // "Field validations were successful"What Happens?
- With
strict: falseat the global level, no fields are mandatory, and missing orundefinedfields are skipped.
Example Use Cases
Strict Mode Validation:
- Ensure that all fields are present and match their expected types (unless
strict: falseis set on a field). - Custom value checks like ensuring
ID !== -1.
- Ensure that all fields are present and match their expected types (unless
Flexible Validation:
- Skip missing fields with
strict: falseon individual fields or globally. - Customize validation logic for each field (e.g., ID range checks, non-empty string checks).
- Skip missing fields with
Error Types
- Typing Error: When a field's type doesn't match the expected type.
- Value Error: When a field's value doesn't meet custom validation rules (e.g.,
ID !== -1).
Security Considerations
✅ Strict validation mode ensures complete validation of object fields.
✅ Custom validation logic gives flexibility to handle complex field rules.
✅ Optional fields with strict: false allow for flexible validation scenarios.
✅ Safe for use in form validation, API response checks, and object integrity validation.
License
MIT License
