Skip to content
/ akar Public

Akar is a typescript first schema validation library offering flexible, reusable schema for multi-purpose use cases

License

Notifications You must be signed in to change notification settings

mahabubx7/akar

Repository files navigation

akar-js

Run Linters Build and Deploy to GitHub Pages

npm-link



Docs are available now! But still working on it.

Installation

npm install akarjs

Example usages

Here is an example of the use cases for this library.

User Schema

import { a, InferSchema, InferSchemaWithConditions } from "akarjs"

const userSchema = a.object({
  name: a.string().min(4),
  age: a.number().integer().min(15).optional()
})

// now, parse it to get: ValidationResult OR ValidationErrors
console.log(userSchema.parse({ name: "Mahabub" }))
// Success: { value: { name: "Mahabub" } }

console.log(userSchema.parse({ name: "Mahabub", age: 26 }))
// Success: { value: { name: "Mahabub", age: 26 } }

console.log(userSchema.parse({ name: "Mahabub", age: 10 }))
// Error: { errors: [{ reason: "Minimum age value is >= 15", field: "age", value: 10 }] }

console.log(userSchema.parse({ age: 10 }))
// Error: { errors: [{ reason: "name is required", field: "name", value: undefiend }] }

// Infer types
type UserTypeNormal = InferSchema<typeof userSchema>
// it will get type as:
/*
type UserTypeNormal = {
  name: string;
  age: number | undefined;
}
*/

type UserTypeWithConditionals = InferSchemaWithConditions<typeof userSchema>
// it will get type as:
/*
type UserTypeWithConditionals = {
  name: string;
} & {
  age?: number;
}
*/

For more, please visit our documentation website: akar.js.org

Made with 💚 by @mahabubx7

Thanks,

~/Mahabub

About

Akar is a typescript first schema validation library offering flexible, reusable schema for multi-purpose use cases

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published