# @devtin/schema-validator

> Zero-dependencies, light-weight library for modeling, validating & sanitizing data

Latest version **3.5.1** (published 2020-08-06) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @devtin/schema-validator
pnpm add @devtin/schema-validator
yarn add @devtin/schema-validator
bun add @devtin/schema-validator
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 3.5.1 |
| Published | 2020-08-06 |
| First published | 2019-12-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 181 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Martin Rafael |
| Maintainers | tin_r |
| Keywords | json, duck-type, duck-typing, sanitize, type, typing, schema, validation, validating, validator, cast, casting, modeling |

## Links

- npm: https://www.npmjs.com/package/@devtin/schema-validator
- Repository: https://github.com/devtin/schema-validator
- Homepage: https://devtin.github.io/schema-validator
- Issues: https://github.com/devtin/schema-validator/issues
- npm.io page: https://npm.io/package/@devtin/schema-validator

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.5.1 (latest) — 2020-08-06
- 1.6.1-next.0 (next) — 2020-01-28
- 3.5.0 — 2020-08-06
- 3.4.0 — 2020-08-02
- 3.3.1 — 2020-08-01
- 3.3.0 — 2020-08-01
- 3.2.0 — 2020-07-29
- 3.1.1 — 2020-07-28
- 3.1.0 — 2020-07-27
- 3.0.4 — 2020-07-26
- 3.0.3 — 2020-07-25
- 3.0.2 — 2020-07-16
- 3.0.1 — 2020-07-15
- 3.0.0 — 2020-07-15
- 2.9.0 — 2020-05-19
- … 39 more at https://npm.io/package/@devtin/schema-validator/versions

## README

<p align="center"><img align="center" width="480" src="https://repository-images.githubusercontent.com/228456718/f4767e00-61e6-11ea-964a-7b02d8dcb48f"/></p>

<div align="center"><h1 align="center">@devtin/schema-validator</h1></div>

<p align="center">
<a href="https://www.npmjs.com/package/@devtin/schema-validator" target="_blank"><img src="https://img.shields.io/npm/v/@devtin/schema-validator.svg" alt="Version"></a>
<a href="https://htmlpreview.github.io/?https://github.com/devtin/schema-validator/blob/master/coverage/lcov-report/index.html"><img src="https://img.shields.io/badge/coverage-99%25-green" alt="Coverage 99%"></a>
<a href="/test/features"><img src="https://github.com/devtin/schema-validator/workflows/test/badge.svg"></a>
<a href="https://gitter.im/schema-validator/community"><img src="https://badges.gitter.im/schema-validator/community.svg"></a>
<a href="https://opensource.org/licenses" target="_blank"><img src="https://img.shields.io/badge/License-MIT-brightgreen.svg"></a>
</p>

<p align="center">
Zero-dependencies, light-weight library (~4.3KB minified + gzipped)<br>
for modeling, validating & sanitizing JavaScript data schemas.
</p>

- [Installation](#installation)
- [About](#about)
- [At a glance](#at-a-glance)
- [Documentation](https://devtin.github.io/schema-validator)

## Installation

```sh
$ npm i @devtin/schema-validator
# or
$ yarn add @devtin/schema-validator
```

## About

Tired of performing duck-type validation while sharing data-schema across different endpoints of my beloved
JavaScript ecosystem, I took some inspiration from the [mongoose](https://mongoosejs.com)'s validation syntax and created
this light-weight library (~4.3KB minified + gzipped) for validating & sanitizing JavaScript data schemas.

## At-a-glance

```js
const { Schema, Transformers } = require('@devtin/schema-validator')
const crypto = require('crypto')

Transformers.Password = {
  loaders: [String],
  parse (pass) {
    return crypto.createHash('sha256').update(pass).digest('hex')
  }
}

// defining the schema
const User = new Schema({
  name: String,
  email: {
    type: String,
    regex: [/[a-z0-9._]+@[a-z0-9-]+\.[a-z]{2,}/, '\'{ value }\' is not a valid e-mail address']
  },
  password: 'Password',
  created: {
    type: Date,
    default: Date.now
  },
  logs: {
    type: Array,
    default () {
      return []
    }
  },
  get lastLog () {
    return this.logs[this.logs.length - 1]
  }
}, {
  methods: {
    log (message) {
      this.$field.logs.push({
        date: new Date(),
        message
      })
      this.$emit('log', message)
    },
    isValidPassword (password) {
      const success = Transformers.Password.parse(password) === this.$field.password
      this.$field.log(`password ${password} ${success ? 'pass' : 'failed'} validation`)
      return success
    }
  }
})

const Martin = User.parse({
  name: 'Martin Rafael',
  email: 'tin@devtin.io',
  password: '123'
})

console.log(Martin.name) // => Martin Rafael
console.log(Martin.email) // => tin@devtin.io
console.log(Martin.password) // => a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
console.log(Martin.created instanceof Date) // => true

Martin.$on('log', console.log)

// [log event fired] => password 456 failed validation
console.log(Martin.isValidPassword('456')) // => false
// [log event fired] => password 123 pass validation
console.log(Martin.isValidPassword('123')) // => true
console.log(Martin.lastLog.message) // => password 123 pass validation

try {
  User.parse({
    name: 'Sandy Papo',
    email: '@huelepega',
    password: '123'
  })
} catch (err) {
  console.log(err instanceof Error) // => true
  console.log(err.message) // => Data is not valid
  console.log(err.errors.length) // => 1
  console.log(err.errors[0] instanceof Error) // => true
  console.log(err.errors[0].message) // => '@huelepega' is not a valid e-mail address
  console.log(err.errors[0].field.fullPath) // => email
}

```


[Read the documentation](https://devtin.github.io/schema-validator)

Have a look at this <a href="https://codepen.io/tin_r/pen/PoqwLMb?editors=0011" target="_blank">codepen playground</a>.

* * *

## License

[MIT](https://opensource.org/licenses/MIT)

&copy; 2019-2020 Martin Rafael <tin@devtin.io>

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