5.0.5 • Published 6 years ago

tean v5.0.5

Weekly downloads
82
License
ISC
Repository
github
Last release
6 years ago

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.

5.0.5

6 years ago

5.0.4

6 years ago

5.0.3

7 years ago

5.0.2

7 years ago

5.0.1

7 years ago

5.0.0

7 years ago

4.0.1

8 years ago

4.0.0

8 years ago

3.0.0

8 years ago

2.2.0

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.0.12

9 years ago

1.0.11

9 years ago

1.0.10

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago