Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Simple object and function parameter validation
gonogo is a minimal, functional assertion library for validating JavaScript values, objects, and function parameters. It was designed to check web component properties during development, but it's simple enough that you could probably use it for any basic validation needs.
Install using npm for use with Node.js and browsers via Browserify, webpack, et. al.
$ npm install --save-dev gonogo
important note about browsers
gonogo
is written with arrow functions, const
/ let
, and template strings, so it'll run in any reasonable modern browser (Chrome, Firefox, Edge, Safari >= 10). For older browsers (Internet Explorer, Safari <= 9), you'll need a transform like es2020.
For, example, with browserify:
$ npm install --save-dev browserify es2020
$ browserify entry.js --global-transform=es2020 > bundle.js
Pass gonogo
a validator function or a schema object with validator functions as its values, and it will return a function you can use to validate values. If a value passes validation, nothing will happen. If the value fails validation, an Error
will be thrown.
For ease, gonogo
comes packaged with a set of chainable validator functions to use.
const gng = require('gonogo')
const validate = gng({
name: gng.string,
count: gng.number.optional,
children: gng.array.lengthOf(3).items(gng.object.keys({name: gng.string}))
})
validate({
name: 'foo',
count: 'not-a-number',
children: [{name: 'bar'}, {name: 'baz'}]
}) // throws
You may wrap up a function up with a set of validator functions / schemas in order to validate any parameters before passing them to the wrapped function.
const gng = require('gonogo')
const gngWrap = require('gonogo/wrap')
const iHave = gngWrap(gng.number, gng.string, (count, things) => {
console.log(`I have ${count} ${things}`)
})
iHave(3000, 'flurbos') // works
iHave('roy', 'flurbos') // throws
Using wrap, you can have a functional web component prop types checker that will work in any framework, non-framework, or non-non-framework.
const gng = require('gonogo')
const validateProps = require('gonogo/wrap')
const Component = validateProps({text: gng.string}, function renderComponent (props) {
const el = document.createElement('div')
const text = document.createTextNode(props.text)
el.appendChild(text)
return el
})
Component({text: 'hello'}) // works
Component({text: 7}) // throws
THIS STUFF IS NOT IMPLEMENTED, BUT IT'S COMING PROBABLY (?)
If you're using gonogo
as a tool during development, removing these assertions in production builds can get you back most of the bytes this library takes up (around 2,500 of them, after minification and gzip).
BROWSERIFY INSTRUCTIONS WILL GO HERE MAYBE SOON BUT ALSO MAYBE NOT SOON
DITTO FOR WEBPACK
validate: function = gng(schema: object|function, [options: object])
gonogo
takes a schema object or validation function. It returns a function that you may use to validate a target.
Given a validation function, the test target value will be passed to the function and a true / false return value will determine if the target is valid. Given a schema object, each key of the object should be a validation function. Those keys will be used to validate their corresponding values of the test target.
If a validation function returns false
, gonogo
will throw. If a validation function return true
, nothing will happen.
Example with validation function:
const validate = gng((value) => value === 'pass')
validate('pass') // nothing happens
validate('fail') // throws
Example with schema object:
const validate = gng({
foo: (value) => value === 'pass',
bar: (value) => value === 'pass',
baz: (value) => value === 'pass'
})
validate({foo: 'pass', bar: 'pass', baz: 'pass'}) // nothing happens
validate({foo: 'pass', bar: 'fail', baz: 'pass'}) // throws
The validation function's toString
method will be called and added to the thrown error. You may override toString
to make validation failure reasons more clear:
const schema = (value) => value === 'pass'
schema.toString = () => '[the value "pass"]'
const validate = gng(schema)
validate('fail') // throws with message '...failed to satisfy [the value "pass"]'
gonogo comes with a collection of validators with chainable methods to use in schemas.
There are seven base validators:
All base validators have certain methods available.
(These examples will use gng.any
)
Allows a value to be undefined
gng.any.optional('foo') // true
gng.any.optional(undefined) // true
gng.any.optional(null) // false
Allows a value to be null
gng.any.nullable('foo') // true
gng.any.nullable(undefined) // false
gng.any.nullable(null) // true
Tests that the value also passes a given predicate
const test = (value) => value === 'foo'
gng.any.and(test)('foo') // true
gng.any.and(test)('bar') // false
Since the built-in validators are just functions, you can pass them into the and
method:
gng.any.and(gng.number)(42) // true
gng.any.and(gng.number)('bar') // false
Tests that the value also passes a given predicate while allowing the base validation to fail
(Note: Because of the way validators are executed, the or
test will run before the base test, which is probably opposite to what you expect. Sorry.)
const isFoo = (value) => value === 'foo'
const isBar = (value) => value === 'bar'
gng.any.and(isFoo).or(isBar)('foo') // true
gng.any.and(isFoo).or(isBar)('bar') // true
gng.any.and(isFoo).or(isBar)('baz') // false
Since the built-in validators are just functions, you can pass them into the or
method:
gng.string.or(gng.number)('foo') // true
gng.string.or(gng.number)(42) // true
gng.string.or(gng.number)(true) // false
Tests that the value does not satisfy given predicate (the opposite of and
)
const test = (value) => value === 'foo'
gng.any.not(test)('foo') // false
gng.any.not(test)('bar') // true
Since the built-in validators are just functions, you can pass them into the pass
method:
gng.any.not(gng.number)(42) // false
gng.any.not(gng.number)('bar') // true
Tests that a value is strictly equal to at least one element of the given array
gng.any.values(['foo', 42])('foo') // true
gng.any.values(['foo', 42])(42) // true
gng.any.values(['foo', 42])('bar') // false
Tests that a given value exists (i.e. is not null
or undefined
). gng.any
has all of the common methods available.
Tests that a value is a string
gng.string('foo') // true
gng.string(12345) // false
gng.string
has all of the common methods available as well as:
Tests that a string has a certain length
gng.string.lengthOf(3)('foo') // true
gng.string.lengthOf(3)('quux') // false
Tests that a string matches a regex (or string). The matcher will be passed to new RegExp
before testing
gng.string.match(/foo/)('__foo__') // true
gng.string.match('foo')('__foo__') // true
gng.string.match(/foo/)('__bar__') // false
Tests that a value is a number
gng.number(12345) // true
gng.number('foo') // false
gng.number
has all of the common methods available.
Tests that a value is a boolean
gng.boolean(false) // true
gng.boolean('foo') // false
gng.boolean
has all of the common methods available.
Tests that a value is a plain object
gng.object({}) // true
gng.object([]) // false
gng.object(null) // false
gng.object
has all gng.any
methods available as well as:
Tests that an object adheres to a schema, where the schema is an object with validation functions for its keys.
const schema = {
foo: gng.string,
bar: gng.number
}
gng.object.keys(schema)({foo: 'foo', bar: 2}) // true
gng.object.keys(schema)({foo: 1, bar: 2}) // false
Tests that a value is an array
gng.array(['0', '1', '2']) // true
gng.array({0: '0', 1: '1', length: 2}) // false
gng.array
has all gng.any
methods available as well as:
Tests that all of an array's items pass a given validation function
gng.array.items(gng.number)([1, 2, 3]) // true
gng.array.items(gng.number)(['1', '2', '3']) // false
Tests that an array has a certain length
gng.array.lengthOf(3)([1, 2, 3]) // true
gng.array.lengthOf(3)([1, 2]) // false
Tests that a value is a function
gng.function(() => 'foo') // true
gng.function('foo') // false
gng.function
has all gng.any
methods available as well as:
Tests that an function has a certain length (arity)
gng.function.lengthOf(3)((a, b, c) => a + b + c) // true
gng.function.lengthOf(3)((a, b) => a + b) // false
Will pass anything, including null
and undefined
gng.any.optional.nullable(ANYTHING_OR_NOTHING) // true
gng.any.or(() => true)(ANYTHING_OR_NOTHING) // true
FAQs
Simple object and function parameter validation
The npm package gonogo receives a total of 0 weekly downloads. As such, gonogo popularity was classified as not popular.
We found that gonogo demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.