Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mahabubx7/akar

Akar is a typescript first schema validation library offering flexible, reusable schema for multi-purpose use cases
https://github.com/mahabubx7/akar

akar akarjs

Last synced: 1 day ago
JSON representation

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

Awesome Lists containing this project

README

        


akar-js

[![Run Linters](https://github.com/mahabubx7/akar/actions/workflows/linter.yml/badge.svg)](https://github.com/mahabubx7/akar/actions/workflows/linter.yml) [![Build and Deploy to GitHub Pages](https://github.com/mahabubx7/akar/actions/workflows/pages.yml/badge.svg)](https://github.com/mahabubx7/akar/actions/workflows/pages.yml)

npm-link




> Docs are available now! But still working on it.

### Installation

```sh [npm]
npm install akarjs
```

### Example usages

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

**User Schema**

```ts
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
// it will get type as:
/*
type UserTypeNormal = {
name: string;
age: number | undefined;
}
*/

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

_For more, please visit our documentation website: [akar.js.org](https://akar.js.org)_

Made with 💚 by @mahabubx7

Thanks,

~/[Mahabub](https://mahabubx7.netlify.app)