Skip to content

Btime/seneca-merge-validate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

seneca-merge-validate

npm node

About

SenecaJS Merge Validate is an abstraction package made to validate/formate a message payload data. This package uses Btime-schema-validate-package to perform validations based on the schemas defined there.

Table of Contents

  1. Setup
    1. Installing
  2. Usage
    1. Example
    2. Language Support
  3. Pushing Versions - Tagging
  4. Tests

Setup

Installing

$ npm i

Usage

$ npm i Btime/seneca-merge-validate#semver:^2 -S

Example

function Plugin () {
  const seneca = this
  const Joi = require('joi')
  const SenecaMergeValidate = require('seneca-merge-validate')
  const senecaMergeValidate = SenecaMergeValidate(seneca)
  const PICK_FIELDS = [
    'field'
  ]

  seneca.add({ role: 'plugin', cmd: 'create', credentials: '*'}, cmd_create)

  function cmd_create (args, done) {
    senecaMergeValidate.validate({
      args,
      pick: PICK_FIELDS,
      schema: { name: args.role, method: args.cmd },
      options: { abortEarly: false }
    })
    .then(params => create(params))
    .then(result => done(null, result))
    .catch(err => done(null, err))
  }

  function getValidateSchema () {
    return {
      field: Joi.any()
        .required()
    }
  }

  function createService (params) {
    return new Promise((resolve, reject) => {
      return reject({ status: false, errors: [] })
      return resolve({ status: true, result: {} })
    })
  }
}

Language Support

Error messages default to the English language, but might be different by setting the language option:

senecaMergeValidate.validate({
  args,
  pick: PICK_FIELDS,
  schema: { name: args.role, method: args.cmd },
  options: {
    abortEarly: false,
    language: 'pt-br'
  }
})

The JOI Language Pack is used in order to allow for such translations.

You can also provide custom messages yourself by defining the language object:

const language = {
  any: {
    required: 'is not optional'
  }
}
senecaMergeValidate.validate({
  args,
  pick: PICK_FIELDS,
  schema: { name: args.role, method: args.cmd },
  options: {
    abortEarly: false,
    language
  }
})

Pushing versions - Tagging

All pushes must come with a new tag. The tag usage must consider semantic versions

  • [major version: incompatible changes].[minor version: compatible with major].[patch version: bug fixes]

Example of Usage

git tag 1.0.0 && git push origin master --tags

Tests

$ npm test

About

SenecaJS Merge Validate

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%