This repository has been archived on 2020-11-15. You can view files and clone it, but cannot push or open issues or pull requests.
schemajs/README.md

217 lines
5.8 KiB
Markdown
Raw Permalink Normal View History

2012-05-28 00:40:03 +00:00
### schemajs [![Build Status](https://secure.travis-ci.org/eleith/schemajs.png)](http://travis-ci.org/eleith/schemajs)
2011-09-24 18:46:29 +00:00
validate objects (including http request params) against a schema. includes express middleware.
### Installing
npm install schemajs
# FEATURES
- validates schema for deep objects and arrays as well
- schema is in JSON, making it portable
- properties, types and filters are extensible
2014-08-24 01:57:17 +00:00
# EXAMPLE USAGE - validate an object
2014-08-24 02:01:55 +00:00
2011-12-10 23:03:16 +00:00
```javascript
var schema = require("./path/to/schemajs");
var model = schema.create(
{
name: {type:"string", filters:"trim", properties:{max:255}, required:true},
email: {type:"email", filters:"trim", required:true},
wins: {type:"int", filters:["trim", "toInt"], default:0},
average: {type:"float", filters:["trim", "toFloat"], default:0}
});
var form = model.validate({name:" your name ", email:" name@example.com "});
// form.valid - boolean, true if object matches schema
// form.data - contains filtered version of the object {name:"your name", email:"name@example.com", wins:0, average:0}
// form.errors - contains associative array of any errors found
```
2011-09-24 18:46:29 +00:00
2015-04-28 08:45:36 +00:00
If you pass `{strict: true}` as additional arguments into `Schema#validate`, the validation will fail if there are additional keys in the object which are not defined in the schema.
2011-09-24 18:54:04 +00:00
# EXAMPLE USAGE - in expressjs, validate the request parameters
2011-12-10 23:03:16 +00:00
```javascript
var schema = require("./path/to/schemajs");
var query = schema.create(
{
wins: {type:"int", filters:"toInt"},
query: {type:"string", filters:"trim", required:true, error:"invalid query"}
});
app.get("/users", schema.middleware(query), function(req, res)
{
// schema validation is attached to req.form
if(req.form.valid)
{
db_lookup({query:req.form.data.query, wins: req.form.data.wins});
}
else
{
next(new Error("invalid request"));
//next(req.form.errors, 400);
}
});
```
2011-09-24 18:46:29 +00:00
# API
## schema.test(schema)
quickly test an individual schema
```javascript
2014-08-24 01:52:03 +00:00
field = schema.test("email@email.com", {type:"email", trim:true, properties:{min:5}});
```
2011-09-24 18:46:29 +00:00
## schema.create(schema)
2011-09-24 19:02:23 +00:00
schema for a simple object of one integer value
2011-12-10 23:03:16 +00:00
```javascript
schema =
{
2011-09-24 19:05:21 +00:00
// [string] value for acceppted parameters of the object you want to test
2011-12-10 23:03:16 +00:00
"wins":
{
// [string (built in types) or function (custom types)] the type declaration for accepted values
"type": "int",
2011-09-24 19:05:21 +00:00
2011-12-10 23:03:16 +00:00
// [string (built in filters) or function (custom filters) or array (mix and match!)]
// OPTIONAL, filters to process and optionally modify values
"filters": ["trim", "toInt", custom_filter],
2011-09-24 19:05:21 +00:00
2011-12-10 23:03:16 +00:00
// [anything]
// OPTIONAL, the default value if none is supplied by the object
"default": 0,
2011-09-24 19:05:21 +00:00
2011-12-10 23:03:16 +00:00
// [boolean]
// OPTIONAL, if true, this parameter must exist to validate
2014-08-24 01:52:03 +00:00
"required": true,
2012-10-08 00:16:06 +00:00
// [boolean]
// OPTIONAL, if true, this parameter can be null, despite any other rules
2014-08-24 01:52:03 +00:00
"allownull": true,
2012-10-08 00:16:06 +00:00
2011-12-10 23:03:16 +00:00
// [object]
// OPTIONAL, functions are custom property checks, else a built in property will be looked up
2014-08-24 01:52:03 +00:00
"properties": {max:100, min:0, special:custom_property},
2011-09-24 19:05:21 +00:00
2011-12-10 23:03:16 +00:00
// [string, object] OPTIONAL, if there is an error, you can override the message that is returned
// use a string or the "default" key to use the default error message
// keys can be any property key that was used as well as "filters", "required", "type"
"error": {max: "too many wins", min: "too few wins", "default": "something is wrong with your wins value"}
}
};
```
2011-09-24 19:02:23 +00:00
schema with embedded schemas for object and array types
2011-12-10 23:03:16 +00:00
```javascript
schema =
{
"user":
{
"type": "object",
"schema":
{
"name": { type: "string", properties: { max: 255 }, required: true},
"email": { type: "email", error: "email is not a valid email address"}
2014-08-24 01:52:03 +00:00
},
2011-12-10 23:03:16 +00:00
"error": "user needs an email and a name"
},
2011-09-24 19:05:21 +00:00
2011-12-10 23:03:16 +00:00
"tags":
{
"type": "array",
"schema":
{
"type": "string"
2014-08-24 01:52:03 +00:00
},
"properties": { max: 10, min: 3},
2011-12-10 23:03:16 +00:00
"error": { max: "too many tags", min: "too few tags" }
}
};
```
2011-09-24 18:46:29 +00:00
## schema.types
2011-12-10 23:03:16 +00:00
built in types
2011-09-24 19:05:47 +00:00
2011-12-10 23:03:16 +00:00
[string+, string (empty string allowed), alpha, alphanum, email, object, array, date, number, int, boolean, url, zipcode]
2011-09-24 19:02:23 +00:00
2011-12-10 23:03:16 +00:00
to extend, add a function onto schema.types that accepts a value and returns a boolean if the type matches
2011-12-10 23:03:16 +00:00
test is run AFTER schema.filters are run
2011-09-24 18:46:29 +00:00
2011-12-10 23:03:16 +00:00
```javascript
schema.types.awesome = function(value)
{
if(isAwesome(value))
return true;
else
return false;
}
```
2011-09-24 18:46:29 +00:00
## schema.filters
2011-12-10 23:03:16 +00:00
built in filters:
2011-09-24 18:54:04 +00:00
2011-12-10 23:03:16 +00:00
[toInt, toFloat, toString, toDate, toBoolean (converts on/off, true/false, 1/0, yes/no to bools), trim, lowercase, uppercase]
2011-09-24 18:54:04 +00:00
2011-12-10 23:03:16 +00:00
to extend, add a function onto schema.filters that takes in a value and returns the updated value
2011-09-24 18:54:04 +00:00
2011-12-10 23:03:16 +00:00
filters are run BEFORE schema.test is run
2011-09-24 19:02:23 +00:00
2011-12-10 23:03:16 +00:00
```javascript
schema.filters.surround = function(value)
{
if(value == needs_surrounding)
return "----" + value + "----";
else
return value;
}
```
2011-09-24 18:46:29 +00:00
## schema.properties
2011-12-10 23:03:16 +00:00
built in properties
2011-09-24 18:46:29 +00:00
2011-12-10 23:03:16 +00:00
[max, min, regex, in]
2011-09-24 18:46:29 +00:00
2011-12-10 23:03:16 +00:00
to extend, add a function onto schema.properties that takes in a value and any additional arguments
2011-09-24 18:54:04 +00:00
2011-12-10 23:03:16 +00:00
properties are run AFTER test and filters
2011-09-24 18:54:04 +00:00
2011-12-10 23:03:16 +00:00
```javascript
schema.properties.unique = function(value)
{
if(isArray(value) && allUnique(value))
return true;
else
return false;
}
2011-09-24 18:54:04 +00:00
2011-12-10 23:03:16 +00:00
schema.properties.notIn = function(value, badwords)
{
if(badwords.indexOf(value) != -1)
return false;
else
return true;
}
```
2011-09-24 18:46:29 +00:00
2015-04-28 11:08:46 +00:00
## Browser usage
You can use it in the browser by using [browserify](http://browserify.org/), however if that's too much work just use <https://wzrd.in/>. There is also an example <examples/cdn.html>
2011-09-24 18:46:29 +00:00
# ideas
- dependency property making one parameters existance depend on another
- more types (phone numbers, credit card, ip address)
- more filters (camelcase, encode/unencode)
- more properties