# skema

> Skema provides a handy & composable way to validate / transform / purify the input data.

Latest version **9.5.5** (published 2020-05-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install skema
pnpm add skema
yarn add skema
bun add skema
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 9.5.5 |
| Published | 2020-05-05 |
| First published | 2014-08-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=4 |
| Dependencies | 6 |
| Unpacked size | 320.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 354 |
| Author | kaelzhang |
| Maintainers | kael |
| Keywords | skema, schema, validators, default, setters, check, validate, type, orm, object, struct, structure, throw, types, validation, validator, typescript, joi, vue, composable, transformer, purifier |

## Links

- npm: https://www.npmjs.com/package/skema
- Repository: https://github.com/kaelzhang/skema
- Homepage: https://github.com/kaelzhang/skema#readme
- Issues: https://github.com/kaelzhang/skema/issues
- npm.io page: https://npm.io/package/skema

## Dependencies (6)

- [err-object](https://npm.io/package/err-object.md) ^5.1.4
- [make-array](https://npm.io/package/make-array.md) ^1.0.5
- [@skema/basic](https://npm.io/package/@skema/basic.md) ^1.0.40
- [core-util-is](https://npm.io/package/core-util-is.md) ^1.0.2
- [promise-faker](https://npm.io/package/promise-faker.md) ^1.2.5
- [promise.extra](https://npm.io/package/promise.extra.md) ^4.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 9.5.5 (latest) — 2020-05-05
- 9.5.4 — 2020-05-05
- 9.5.1 — 2019-09-20
- 9.5.0 — 2019-09-20
- 9.4.3 — 2019-09-05
- 9.4.2 — 2019-07-17
- 9.4.1 — 2019-03-29
- 9.4.0 — 2019-03-29
- 9.3.9 — 2019-03-11
- 9.3.8 — 2019-03-11
- 9.3.7 — 2018-10-16
- 9.3.5 — 2018-06-29
- 9.3.4 — 2018-06-29
- 9.3.3 — 2018-06-19
- 9.3.1 — 2018-02-02
- … 49 more at https://npm.io/package/skema/versions

## README

[![Build Status](https://travis-ci.org/kaelzhang/skema.svg?branch=master)](https://travis-ci.org/kaelzhang/skema)
[![Coverage](https://codecov.io/gh/kaelzhang/skema/branch/master/graph/badge.svg)](https://codecov.io/gh/kaelzhang/skema)

# skema

`skema` provides a handy and composable way to validate/transform JavaScript variables:

- **Supports both async and sync flows.** Skema has two working modes to support either async or sync validators, setters, etc, making it capable with much more complicated challenges.

- **NOT only type checker.** Unlike [TypeScript](https://www.typescriptlang.org/), [joi](https://github.com/hapijs/joi), and many others, Skema is not only a JavaScript type checker, but also a good solution for your [Anti-Corruption Layer (ACL)](https://docs.microsoft.com/en-us/azure/architecture/patterns/anti-corruption-layer) to transform and purify the input data. And Skema could also be configured as a simple schema validator too.

- **Pluggable basic types.** Even basic types such as `Number` could also be replaced and customized if using Skema. Actually, in the core of Skema, there is NOT a single definition of one type.

- **Powerful custom types.** Every single type is able to be customized that you can handle almost everything including descriptor, conditions, default values, validators and so on.

- **Composable structures.** You could build a much bigger schema with the small ones into the whole world.

## Install

```sh
npm i skema
```

## Basic Usage

[🔬 Live Demo with JsFiddle](https://jsfiddle.net/kaelzhang/0r3g4ogj/)

```js
import {shape} from 'skema'

// Schema definitions are ONLY objects.
const User = shape({
  id: 'number?',
  name: String
})

// Then use these definitions to purify our data.
const user = User.from({
  id: '1',
  name: 'Steve'
})

console.log(user)
// {
//   id: 1,
//   name: 'Steve'
// }

user.id = 'boooom!'
// throw TypeError
// - message: 'not a number'
// - code: 'VALIDATION_FAILS'
```

## Documentations

- API References
  - [APIs](./doc/apis.md)
  - [Builtin Types and How to Change Them](./doc/builtins.md)
- [Shape Definition](./doc/shape.md)
- [Working Mode: Sync or Async](./doc/working-mode.md)
- [Assign a Property after `from()`](./doc/assign.md)
- [Error Handling](./doc/errors.md)
- [Contributing](./doc/contributing.md)

## Many Examples

- **Shape Definition**
  - [Purify an Object Against a Shape🔬](https://jsfiddle.net/kaelzhang/0wosjdo9/)
  - [Default Value of a Property🔬](https://jsfiddle.net/kaelzhang/zhu8crde/)
  - [Optional Properties🔬](https://jsfiddle.net/kaelzhang/pesgkw9c/)
  - [Skip Processing a Property🔬](https://jsfiddle.net/kaelzhang/joq5vdd7/)
  - [Properties Descriptors: Non-Enumerable Properties, ...🔬](https://jsfiddle.net/kaelzhang/yhj2xj72/)
- **Type Definition**
  - [Basic Validation](./examples/basic-validation.js) | [Live Demo🔬 ](https://jsfiddle.net/kaelzhang/2au1on62/)
  - [Async Validation](./examples/async-validation.js) | [Live Demo🔬](https://jsfiddle.net/kaelzhang/1rr5asyb/)
  - [Multiple Validators](./examples/multiple-validators.js)
  - [Basic Usage of Setters](./examples/setters.js)
  - [Inherit Another Type](./examples/type-inheritance.js)
  - [Declare a Type Alias to Make a Shortcut (Live Demo🔬)](https://jsfiddle.net/kaelzhang/7d5u4z0s/)
  - [Use Skema as the Strict Type Checker](./examples/strict-basics.js) | [Live Demo🔬](https://jsfiddle.net/kaelzhang/14y4s0e9/)
- [Errors🔬](https://jsfiddle.net/kaelzhang/scvLn8Ly/)

## Related Packages

- [@skema/basic](https://www.npmjs.com/package/@skema/basic) The default built-in javascript types of skema.

## License

MIT

---
_Source: https://npm.io/package/skema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
