1.7.0 • Published 5 years ago

jsonql-params-validator v1.7.0

Weekly downloads
2
License
ISC
Repository
-
Last release
5 years ago

jsonql-params-validator

This is for use with jsonql server and client side for type validation, using the contract as reference point.

The default export is in umd format. There is also an cjs version in the dist folder named jsonql-params-validator.cjs.js for use with node.js

For validation you can use the following methods:

import {
  validateSync,
  validateAsync,
  normalizeArgs,
  isObject
} from 'jsonql-params-validator'
  • validateSync this is the only one that we need to call validate(args, params) = Array
  • validateAsync same as above but return a promise validateAsync(args, params) = Promise
  • normalizeArgs this is export main for use on the client side normalizeArgs(args, params)
  • isObject this is a wrapper of our checkIsObject method to check if the argument is a plain object

For detail please refer to documentation (work in progress at the moment)

If you have problem to import the module, you might need to do

jsonqlParamsValidator.default

Utility for checking configuration

We also have several methods for checking configuration input, and make sure they are what we expected. The reason we create this is during years of development, how often you find a bug that you never come across when user is using your software - and it turns out the end user is passing a wrong configuration property that cause it? Or the wrong type of the value you expect it. And we heavily use this across our entire jsonql tool set to ensure developer are passing the right configurations, hence to eliminated potential bug.

const {
  checkConfig, // sync version
  checkConfigAsync, // return promise
  constructConfig
} = require('jsonql-params-validator')

TBC about how to use them

Custom Errors for checking

We also export 3 custom errors that we throw from within the validation function. And you can use them to check against your code to know what went wrong with your configuration. This will be in a separate package jsonql-errors

const {
  JsonqlTypeError,
  JsonqlEnumError ,
  JsonqlCheckerError
} = require('jsonql-errors')

Example:

import { BOOLEAN_TYPE, NUMBER_TYPE, STRING_TYPE } from 'jsonql-constants'

let appProps = {
  importantProp: constructConfig(true, BOOLEAN_TYPE),
  anotherProp: constructConfig(123, NUMBER_TYPE),
  anythingProp: constructConfig('*', [NUMBER_TYPE, STRING_TYPE])
}
let constProps = {
  propDontWantToChange: 'I-must-be-here!'
}

let config = {importantProp: 'ok'}; // passing a string?!

checkConfigAsync(config, appProps, constProps)
  .then(result => {
    // do your thing
  })
  .catch(error => {
    if (error instanceof JsonqlTypeError) {
      console.error(`The config value for ${error.message} you passed is wrong!`)
    }
  })

Joel Chu

NEWBRAN LTD / TO1SOURCE CN (c) 2019

1.7.0

5 years ago

1.6.3

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.13

5 years ago

1.4.12

5 years ago

1.4.11

5 years ago

1.4.10

5 years ago

1.4.9

5 years ago

1.4.8

5 years ago

1.4.7

5 years ago

1.4.6

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.9

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-beta.5

6 years ago

1.0.0-beta.4

6 years ago

1.0.0-beta.3

6 years ago

1.0.0-beta.2

6 years ago

1.0.0-beta.1

6 years ago

1.0.0-alpha.5

6 years ago

1.0.0-alpha.4

6 years ago

1.0.0-alpha.3

6 years ago

1.0.0-alpha.2

6 years ago

1.0.0-alpha.1

6 years ago