offshore-validator
offshore-validator is a javascript library that lets you define strict types.
This makes it really useful for things like:
- Form validation on the client or server
- Ensuring compliance with an API
- Ensuring that the proper arguments were passed into a function or command-line script
- Validating objects before storing them in a data store
- Normalizing polymorphic behaviors
Adds support for strongly typed arguments, like http://lea.verou.me/2011/05/strongly-typed-javascript/, but goes a step further by adding support for array and object sub-validation. It's also the core validation library for the offshore ecosystem.
(Built on top of the great work with https://github.com/chriso/validator.js)
Installation
Client-side
node.js
npm install offshore-validator
Basic Usage
var offshore-validator = ; var userData = 'some string'; // This will guarantee that userData is a string// If it's not, an error will be thrownuserData = offshore-; // If you want to handle the error instead of throwing it, use a callbackoffshore-;
Objects
// Limit data to match these requirementsvar requirements = offshore-; // Unvalidated data from the uservar userData = name: 'Elvis' avatar: path: '/tmp/2Gf8ahagjg42.jpg' name: '2Gf8ahagjg42.jpg' size: 382944 type: 'image/jpeg' ; // Verify that the userData at least contains your requirements// It can have EXTRA keys, but it MUST have the keys you specifyoffshore-;
Custom rules
Offshore-validator also supports custom validation rules.
// Define a compound validation rule using offshore-validator typesoffshore-validator; // Define a custom rule using a functionoffshore-validator; // you can use your new validation rules like any standard offshore-validator data type:offshore-; offshore-;
We bundled a handful of useful defaults:
offshore-;
The example below demonstrates the complete list of supported default data types:
offshore-;
Functions
TODO: Support for functions is incomplete. If you'd like to contribute, please reach out at @balderdashy!
It also has built-in usage to verify the arguments of a function. This lets you be confident that the arguments are what you expect.
$get = offshore-; // The following usage will throw an error because agasdg is not urlish$ // You can use the same callback from above in your definition to handle the error yourself$get = offshore-;
Multiple usages and Argument normalization
But sometimes you want to support several different argument structures.
And to do that, you have to, either explicitly or implicitly, name those arguments so your function can know which one was which, irrespective of how the arguments are specified.
Here's how you specify multiple usages:
// This will create a copy of the function that only allows usage that explicitly declares an idvar getById = offshore- // Here you can define your named arguments as temporal custom data types, // each with their OWN validation rules // To pass valiation, the user arguments must match at least one of the usages below // and each argument must pass its validation definition above ;
Call it any way you want
Now the cool part. You can call your new function any of the following ways:
$;$;
Default values
You can also specify default values. If it's not required, if a value listed in defaults is undefined, the default value will be substituted. A value should not have a default AND be required-- one or the other.
Here's an example for an object's keys:
offshore-
And here's an example for a function's arguments:
offshore- ;
Asynchronous Usage / Promises
Offshore-validator can also help you normalize your synchronous and asynchronous functions into a uniform api. It allows you to support both last-argument-callback (Node standard) and promise usage out of the box.
TODO
Express/Connect Usage
app; // ... { // Any errors will be handled by Express/Connect var params = reqoffshore-validator; // Do stuff here // This could be anything-- we chose to demonstrate a create method // in this case from our favorite ORM, Offshore (https://github.com/Atlantis-Software/offshore) async; }
Tests
npm test
The MIT License (MIT)
Copyright © 2012- Mike McNeil Copyright © 2015- Atlantis Software
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.