readme styling
This commit is contained in:
parent
290e189abf
commit
b4cfe49ba6
173
README.md
173
README.md
|
@ -53,67 +53,79 @@ validate objects (including http request params) against a schema. includes expr
|
|||
|
||||
## schema.create(schema)
|
||||
|
||||
// schema for a simple object of one integer value
|
||||
schema =
|
||||
{
|
||||
// [string] value for acceppted parameters of the object you want to test
|
||||
"wins":
|
||||
{
|
||||
// [string (build in types) or function (custom types)] the type declaration for accepted values
|
||||
"type": "int",
|
||||
|
||||
// [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],
|
||||
|
||||
// [anything] OPTIONAL, the default value if none is supplied by the object
|
||||
"default": 0,
|
||||
|
||||
// [boolean] OPTIONAL, if true, this parameter must exist to validate
|
||||
"required": true
|
||||
|
||||
// [object] OPTIONAL, functions are custom property checks, else a built in property will be looked up
|
||||
"properties": {max:100, min:0, special:custom_property}
|
||||
|
||||
// [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"}
|
||||
}
|
||||
};
|
||||
|
||||
// schema looking at two complex values (object and an array) with embedded schemas
|
||||
schema =
|
||||
schema for a simple object of one integer value
|
||||
|
||||
'''javascript
|
||||
schema =
|
||||
{
|
||||
// [string] value for acceppted parameters of the object you want to test
|
||||
"wins":
|
||||
{
|
||||
// [string (built in types) or function (custom types)] the type declaration for accepted values
|
||||
"type": "int",
|
||||
|
||||
// [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],
|
||||
|
||||
// [anything]
|
||||
// OPTIONAL, the default value if none is supplied by the object
|
||||
"default": 0,
|
||||
|
||||
// [boolean]
|
||||
// OPTIONAL, if true, this parameter must exist to validate
|
||||
"required": true
|
||||
|
||||
// [object]
|
||||
// OPTIONAL, functions are custom property checks, else a built in property will be looked up
|
||||
"properties": {max:100, min:0, special:custom_property}
|
||||
|
||||
// [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"}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
schema with embedded schemas for object and array types
|
||||
|
||||
'''javascript
|
||||
schema =
|
||||
{
|
||||
"user":
|
||||
{
|
||||
"type": "object",
|
||||
"schema":
|
||||
{
|
||||
"user":
|
||||
{
|
||||
"type": "object",
|
||||
"schema":
|
||||
{
|
||||
"name": { type: "string", property: { max: 255 }, required: true},
|
||||
"email": { type: "email", error: "email is not a valid email address"}
|
||||
}
|
||||
"error": "user needs an email and a name"
|
||||
},
|
||||
|
||||
"tags":
|
||||
{
|
||||
"type": "array",
|
||||
"schema":
|
||||
{
|
||||
"type": "string"
|
||||
}
|
||||
"properties": { max: 10, min: 3}
|
||||
"error": { max: "too many tags", min: "too few tags" }
|
||||
}
|
||||
};
|
||||
"name": { type: "string", properties: { max: 255 }, required: true},
|
||||
"email": { type: "email", error: "email is not a valid email address"}
|
||||
}
|
||||
"error": "user needs an email and a name"
|
||||
},
|
||||
|
||||
"tags":
|
||||
{
|
||||
"type": "array",
|
||||
"schema":
|
||||
{
|
||||
"type": "string"
|
||||
}
|
||||
"properties": { max: 10, min: 3}
|
||||
"error": { max: "too many tags", min: "too few tags" }
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## schema.types
|
||||
|
||||
// built in types
|
||||
[string, alpha, alphanum, email, object, array, date, number, int, boolean, url, zipcode]
|
||||
built in types
|
||||
[string, alpha, alphanum, email, object, array, date, number, int, boolean, url, zipcode]
|
||||
|
||||
// to extend, add a function onto schema.types that accepts a value and returns a boolean if the type matches
|
||||
// test is run AFTER schema.filters are run
|
||||
to extend, add a function onto schema.types that accepts a value and returns a boolean if the type matches
|
||||
test is run AFTER schema.filters are run
|
||||
|
||||
'''javascript
|
||||
schema.types.awesome = function(value)
|
||||
{
|
||||
if(isAwesome(value))
|
||||
|
@ -121,6 +133,7 @@ validate objects (including http request params) against a schema. includes expr
|
|||
else
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
## schema.filters
|
||||
|
||||
|
@ -132,13 +145,17 @@ validate objects (including http request params) against a schema. includes expr
|
|||
|
||||
filters are run BEFORE schema.test is run
|
||||
|
||||
schema.filters.surround = function(value)
|
||||
{
|
||||
if(value == needs_surrounding)
|
||||
return "----" + value + "----";
|
||||
else
|
||||
return value;
|
||||
}
|
||||
'''javascript
|
||||
schema.filters.surround = function(value)
|
||||
{
|
||||
if(value == needs_surrounding)
|
||||
return "----" + value + "----";
|
||||
else
|
||||
return value;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## schema.properties
|
||||
|
||||
|
@ -150,21 +167,23 @@ validate objects (including http request params) against a schema. includes expr
|
|||
|
||||
properties are run AFTER test and filters
|
||||
|
||||
schema.properties.unique = function(value)
|
||||
{
|
||||
if(isArray(value) && allUnique(value))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
'''javascript
|
||||
schema.properties.unique = function(value)
|
||||
{
|
||||
if(isArray(value) && allUnique(value))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
schema.properties.notIn = function(value, badwords)
|
||||
{
|
||||
if(badwords.indexOf(value) != -1)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
schema.properties.notIn = function(value, badwords)
|
||||
{
|
||||
if(badwords.indexOf(value) != -1)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# ideas
|
||||
|
||||
|
|
Reference in New Issue