Skip to content

Commit

Permalink
Add validators.all shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Serby committed Sep 27, 2016
1 parent fcbf302 commit 0415ef2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ the module within your application whether you are storing your objects or not.

## Changelog

### v3.1.0

* Fixed a bug where `stripUnknownProperties()` was not stripping out properties of type array that were null.
* Introduces shorthand for `schema.validators.all = []`. Now `schema.validators = []` is equivalent.

### v3.0.0

This version prevents you from using arrays and objects for `defaultValue`. Now
Expand Down Expand Up @@ -55,7 +60,7 @@ var contactSchema = schemata({
* **type**: (optional) The javascript type that the property value will be coerced into via the **cast()** and **castProperty()** functions. If this is omitted the property will be of type String. Type can be any of the following: String, Number, Boolean, Array, Object, Date or another instance of a schemata schema.
* **defaultValue**: (optional) The property value return when using **makeDefault()** If this is a function, it will be the return value.
* **tag[]**: (optional) Some functions such as **cast()** and **stripUnknownProperties()** take a tag option. If this is passed then only properties with that tag are processed.
* **validators{}**: (optional) A object containing all the validator set for this property. By default the validator set 'all' will be used by **validate()**. schemata gives you the ability defined any number of validator sets, so you can validate an object in different ways.
* **validators{}**: (optional) A object containing all the validator set for this property. By default the validator set 'all' will be used by **validate()**. schemata gives you the ability defined any number of validator sets, so you can validate an object in different ways. Since 3.1, if you only want one set of validators you can set `.validators = [ validatorA, validatorB ]` as a shorthand.

### Creating a new object

Expand All @@ -72,7 +77,7 @@ var blank = contactSchema.makeBlank()
### Creating a new object with the default values

```js
var default = contactSchema.makeDefault();
var default = contactSchema.makeDefault()
```
{
name: null,
Expand All @@ -91,7 +96,7 @@ schemata scheme.
var stripped = contactSchema.stripUnknownProperties({
name: 'Dom',
extra: 'This should not be here'
});
})
```
{
name: 'Dom'
Expand Down Expand Up @@ -143,10 +148,10 @@ For a comprehensive set of validators including: email, integer, string length,

### Cast an object to the types defined in the schema

Type casting is done in schemata using the **cast()** and **castProperty()** functions. **cast()** is used for when you want to cast multiple properties against a schama, **castProperty()** is used if you want to cast one property and explicitly provide the type.
Type casting is done in schemata using the **cast()** and **castProperty()** functions. **cast()** is used for when you want to cast multiple properties against a schema, **castProperty()** is used if you want to cast one property and explicitly provide the type.

```js
var schemata = require('schemata');
var schemata = require('schemata')

var person = schemata({
name: {
Expand All @@ -167,7 +172,7 @@ var person = schemata({
extraInfo: {
type: Object
}
});
})

var objectToCast = {
name: 123456,
Expand All @@ -176,9 +181,9 @@ var objectToCast = {
birthday: '13 February 1991',
friends: '',
extraInfo: undefined
};
}

var casted = person.cast(objectToCast);
var casted = person.cast(objectToCast)
// casted = {
// name: '123456',
// age: 83,
Expand All @@ -196,7 +201,7 @@ If you want to output the name of a schema property in a human-readable format t
Consider the following example:

```js
var schemata = require('schemata');
var schemata = require('schemata')

var address = schemata({
addressLine1: {},
Expand All @@ -207,12 +212,12 @@ var address = schemata({
addressLine4: {
name: 'Region'
}
});
})

console.log(address.propertyName('addressLine1'));
console.log(address.propertyName('addressLine1'))
// Returns 'Address Line 1' because there is no name set

console.log(address.propertyName('addressLine3'));
console.log(address.propertyName('addressLine3'))
// Returns 'Town' because there is a name set
```

Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "schemata",
"description": "Define, create, and validate your business objects, based on specified schema.",
"description": "Define, create, and validate your business objects, based on well defined schema.",
"version": "3.1.0",
"author": "Paul Serby <paul@serby.net>",
"publishConfig": {
Expand All @@ -23,15 +23,15 @@
"dependencies": {
"async": "^2.0.1",
"is-primitive": "^2.0.0",
"piton-string-utils": "^0.3.0",
"validity": "^0.0.3"
"piton-string-utils": "^0.3.0"
},
"devDependencies": {
"istanbul": "^0.4.5",
"jscs": "^3.0.7",
"jshint": "^2.9.3",
"mocha": "^3.1.0",
"should": "^11.1.0"
"should": "^11.1.0",
"validity": "^0.0.3"
},
"keywords": [
"entity",
Expand All @@ -41,8 +41,8 @@
"repository": "git://github.com/serby/schemata.git",
"main": "schemata",
"scripts": {
"lint": "jshint . && ./node_modules/.bin/jscs . -c ./.jscsrc",
"pretest": "npm run-script lint",
"lint": "jshint . && jscs . -c ./.jscsrc",
"pretest": "npm run lint",
"test": "istanbul cover ./node_modules/.bin/_mocha -- -R spec -r should",
"posttest": "istanbul check-coverage --statements 95 --branches 85 --functions 100 --lines 95 && rm -rf coverage",
"prepublish": "npm test && npm prune"
Expand Down
10 changes: 6 additions & 4 deletions schemata.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,14 @@ Schemata.prototype.validateRecursive = function (parent, entityObject, set, tag,
*/
function validateSimpleProperty(cb) {

// This property has no validators, or none specified for the given set
if (property.validators === undefined || !Array.isArray(property.validators[set])) return cb()

var validators = property.validators[set]
// This allows for validator to simply be an array. New in v3.1!
var validators = Array.isArray(property.validators) &&
set === 'all' ? property.validators : property.validators && property.validators[set]
, errorName = property.name === undefined ? stringUtils.decamelcase(key) : property.name

// This property has no validators, or none specified for the given set
if (validators === undefined || !Array.isArray(validators)) return cb()

async.forEach(validators, function (validator, validateCallback) {
if (errors[key]) return validateCallback()
if (validator.length === 5) {
Expand Down
2 changes: 1 addition & 1 deletion test/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function createKidSchema() {
function createToySchema() {
return schemata(
{ name: { type: String }
, label: { type: String, validators: { all: [ validity.required ] } }
, label: { type: String, validators: [ validity.required ] }
})
}

Expand Down

0 comments on commit 0415ef2

Please sign in to comment.