3.5.1 • Published 4 years ago

@devtin/schema-validator v3.5.1

Weekly downloads
56
License
MIT
Repository
github
Last release
4 years ago

Installation

$ 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's validation syntax and created this light-weight library (~4.3KB minified + gzipped) for validating & sanitizing JavaScript data schemas.

At-a-glance

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

Have a look at this codepen playground.


License

MIT

© 2019-2020 Martin Rafael tin@devtin.io

3.5.1

4 years ago

3.5.0

4 years ago

3.4.0

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.9.0

4 years ago

2.8.5

4 years ago

2.8.4

4 years ago

2.8.3

4 years ago

2.8.2

4 years ago

2.8.1

4 years ago

2.8.0

4 years ago

2.7.0

4 years ago

2.6.0

4 years ago

2.5.0

4 years ago

2.4.6

4 years ago

2.4.5

4 years ago

2.4.4

4 years ago

2.4.3

4 years ago

2.4.2

4 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.4

4 years ago

2.1.2

4 years ago

2.1.3

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.6.1-next.0

4 years ago

1.2.3-next.2

4 years ago

1.2.3-next.0

4 years ago

1.2.3-next.1

4 years ago

1.2.1-next.0

4 years ago

1.2.1

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.1

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago