npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonql-params-validator

v1.7.0

Published

JSONQL parameters validator written in ES6+ to use with the client / server

Downloads

95

Readme

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