AkiVal

A schema validation library for Akinon applications. Wraps Yup with re-exports for convenient access to all validation utilities.

Installation

pnpm add @akinon/akival

Basic 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

Export
Description

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

Export
Description

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

Export
Description

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

Export
Description

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

Method
Description

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

Method
Description

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

  1. Infer types from schemas - Use InferType to keep types in sync with validation

  2. Use with Akiform - Combine with yupResolver for form validation

  3. Set global locale - Configure setLocale for consistent error messages across the app

  4. Keep schemas in separate files - Export schemas from dedicated files for reusability

  5. Use conditional validation - Leverage .when() for dynamic field requirements

More Information

For complete Yup documentation, visit Yup GitHubarrow-up-right.

Last updated

Was this helpful?