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

tean

v5.0.5

Published

Strict data validation and normalization

Downloads

75

Readme

Tean

Tean is a declarative, strict and asynchronous way to validate and normalize data. The markup uses plain Javascript objects, so it's easy to pick up and read. Everything is strictly validated and normalized, so you know exactly what you're getting, no exceptions. This makes code more secure and can completely eliminate unexpected type errors. Tean is designed for use with io.js and can be installed by running npm install tean.

##Quick Examples

// simple validation
tean.object({breakfast: "string"}, {breakfast: "bacon"}, (isValid, result) => {
  console.log(isValid); // true
  console.log(result);  // {breakfast: "bacon"}
});

tean.object({breakfast: "string"}, {breakfast: null}, (isValid, result) => {
  console.log(isValid); // false
  console.log(result);  // ["breakfast (null) is not a string"]
});

// optional parameters
tean.object({breakfast: "string=waffles", addSyrup: "bool=true"}, {breakfast: "pancakes"}, (isValid, result) => {
  console.log(isValid); // true
  console.log(result); // {breakfast: "pancakes", addSyrup: true}
  // Note that the original object is not altered! Normalized and validated data is passed into "result" in the callback
});

There are lots more examples in test/test.js !

##Documentation ###expressRequest(req, res, next) expressRequest is used as middleware for Express. Replies to request with a 400 error if validation fails. Validated and normalized data is attached to the request object as req.safeData.

###object(map, data, callback(isValid, result)) object is used to validate data and provide it normalized as result in the callback. object does not alter data. result contains only object keys that have been validated. This means any object keys not present in the map will not be copied into result. Also, any fields not present in data that have default values in map will be present in result with the defined default. If validated is false, safeData is an array of failure messages indicating which properties failed and why.

###json(map, jsonData, callback(validate, safeData)) json is the same as object, but it accepts a json string instead of a Javascript object.

###addBaseTypes([typeNames]) Adds all base validators to tean. Normally tean.addBaseTypes() should be called before any other tean functions. The following types are registered by default with tean by calling this function: *bool - Boolean true or false *int - Integer *json - String that can be parsed into an object *number - Floating point number. Can also be an integer *string - String *email - String that is an email address Optionally, an array of list names can be passed in e.g. ["int", "number"]) if you only want to register a subset of base types.

###addType(typeName, validateFunction, formatDefaultFunction) Register a validator with tean. This must happen before the type can be used in a map. typeName is the new type's name, validateFunction will be called on data that is to be validated/normalized. formatDefaultFunction accepts a string that is to be parsed into a default value if one is required.

###extendType(baseType, typeName, args) Register a new type by setting parameters on an existing one. baseType is a type that has already been registered with tean. typeName is the name given to the extended type. args are the parameters passed to the validator. extendType can be useful if you find yourself adding the same arguments to type in a map over and over again. For example, if you need to accept an integer 1 through 15 as a menuItem in several places, you could write "{menuItem: "int(0, 15)"} everywhere in your code. However, it's more maintainable to extend the int type by creating a new "menuItem" type that always has the range 1 through 15 predefined.

###requestLogFunction Assign a function to tean.requestLogFunction if you would like tean to log parameters for express requests. The function should accept a string as its only argument.

###failureLogFunction Assign a function to tean.requestLogFunction if you would like tean to log failures when an express request fails validation. The function should accept a string as its only argument.