Primate
Guides Validation

Validate with Pema

Pema stands Primate schema. Use pema for type-safe validation of data.

Pema provides composable validators for common types.

Basic usage

Import and use validators.

import pema from "pema";
import string from "pema/string";
import number from "pema/number";

const schema = pema({
  name: string.min(1),
  age: number.min(0),
});

const data = schema.parse({ name: "John", age: 30 });

Built-in validators

  • string: String validation
  • number: Number validation
  • boolean: Boolean validation
  • array: Array validation
  • object: Object validation

Chaining

Chain validators for complex rules.

const emailSchema = string.min(1).max(255).email();
const ageSchema = number.integer().min(0).max(150);

Error handling

Validation throws ParseError with details.

try {
  schema.parse(input);
} catch (error) {
  console.log(error.errors); // detailed errors
}