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 typix
Or using Yarn:
yarn add typix
How 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
ID
is not-1
). - Supports strict validation mode, where all fields must be present and match their types unless specified otherwise.
- Individual fields can have a
strict
property 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?
Typix
checks if theID
is anumber
and not equal to-1
.- Validates
NAME
as astring
andAGE
as 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 errors
What Happens?
- If the
ID
is 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
NAME
field is optional because itsstrict
property isfalse
, soundefined
is allowed. - Other fields (
ID
andAGE
) 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: false
at the global level, no fields are mandatory, and missing orundefined
fields are skipped.
Example Use Cases
Strict Mode Validation:
- Ensure that all fields are present and match their expected types (unless
strict: false
is 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: false
on 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