AkiVal
A schema validation library for Akinon applications. Wraps Yup with re-exports for convenient access to all validation utilities.
Installation
pnpm add @akinon/akivalBasic Usage
import { object, string, number, type InferType } from '@akinon/akival';
// Define a schema
const productSchema = object({
name: string().required(),
price: number().positive().required(),
description: string().optional()
});
// Infer TypeScript type from schema
type Product = InferType<typeof productSchema>;
// Validate data
const validateProduct = async (data: unknown) => {
try {
const product = await productSchema.validate(data);
return { success: true, data: product };
} catch (error) {
return { success: false, error };
}
};API Reference
Akival re-exports all Yup utilities. Below are the most commonly used exports.
Schema Builders
string()
String schema
number()
Number schema
boolean() / bool()
Boolean schema
date()
Date schema
array()
Array schema
object()
Object schema
mixed()
Mixed/any type schema
lazy()
Lazy evaluation schema
ref()
Reference to another field
Types
InferType<T>
Infer TypeScript type from schema
AnySchema
Any schema type
AnyObjectSchema
Any object schema type
ISchema
Schema interface
LocaleObject
Locale configuration type
Schema Classes
Schema
Base schema class
StringSchema
String schema class
NumberSchema
Number schema class
BooleanSchema
Boolean schema class
DateSchema
Date schema class
ArraySchema
Array schema class
ObjectSchema
Object schema class
MixedSchema
Mixed schema class
Utilities
setLocale()
Set global validation messages
akival
Full Yup namespace for advanced usage
Parsing and Transforms
Schemas can parse and transform input values. Use cast() to coerce values without validation:
Custom Transforms
Custom Tests
Create custom validation tests for complex logic:
Schema Immutability
Schemas are immutable. Each method call returns a new schema object:
This allows safe reuse and composition of schemas.
String Validation
Common String Methods
required(message?)
Field is required
min(limit, message?)
Minimum length
max(limit, message?)
Maximum length
email(message?)
Valid email format
url(message?)
Valid URL format
matches(regex, message?)
Match regex pattern
trim()
Trim whitespace
lowercase()
Transform to lowercase
uppercase()
Transform to uppercase
nullable()
Allow null
optional()
Field is optional
Number Validation
Common Number Methods
required(message?)
Field is required
min(limit, message?)
Minimum value
max(limit, message?)
Maximum value
positive(message?)
Must be positive
negative(message?)
Must be negative
integer(message?)
Must be an integer
moreThan(value, message?)
Greater than value
lessThan(value, message?)
Less than value
Object Validation
Array Validation
Conditional Validation
Cross-Field Validation
Custom Validation Messages
Per-Field Messages
Global Locale
With i18n Integration
Use setLocale with your translation function for dynamic messages:
Integration with Akiform
Use Akival schemas with Akiform for form validation. The akivalResolver is exported from @akinon/akiform:
Type Inference
Async Validation
Transform Values
Best Practices
Infer types from schemas - Use
InferTypeto keep types in sync with validationUse with Akiform - Combine with
yupResolverfor form validationSet global locale - Configure
setLocalefor consistent error messages across the appKeep schemas in separate files - Export schemas from dedicated files for reusability
Use conditional validation - Leverage
.when()for dynamic field requirements
Related
Akiform - Form library with validation support
Akiform Builder - Schema-driven form builder
More Information
For complete Yup documentation, visit Yup GitHub.
Last updated
Was this helpful?

