Skip to content

Commit

Permalink
Draft system schema.
Browse files Browse the repository at this point in the history
Add multiple types example.
  • Loading branch information
tmpfs committed Sep 17, 2015
1 parent 01beba8 commit c02c8c9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
28 changes: 27 additions & 1 deletion EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Table of Contents
* [message-override](#message-override)
* [message](#message)
* [min](#min)
* [multiple-types](#multiple-types)
* [multiple](#multiple)
* [pattern](#pattern)
* [placeholder](#placeholder)
Expand Down Expand Up @@ -444,6 +445,31 @@ schema.validate(source, function(err, res) {
[ { [Error: func must have at least 1 arguments] field: 'func', reason: { id: 'min' } } ]
```

#### multiple-types

* [doc/example/multiple-types](https://github.com/freeformsystems/async-validate/blob/master/doc/example/multiple-types.js).

```javascript
// validate a field as one of multiple types
var Schema = require('async-validate')
, descriptor = {
flag: {type: ['boolean', Boolean], required: true}
}
, source = {flag: 'foo'}
, schema;

require('async-validate/plugin/all');

schema = new Schema(descriptor);
schema.validate(source, function(err, res) {
console.dir(res.errors);
});
```

```
[ { [Error: flag is not one of the allowed types boolean, Boolean] field: 'flag', reason: { id: 'type' } } ]
```

#### multiple

* [doc/example/multiple](https://github.com/freeformsystems/async-validate/blob/master/doc/example/multiple.js).
Expand Down Expand Up @@ -701,7 +727,7 @@ schema.validate(source, opts, function(err, res) {
```

```
[ { [Error: email: could not resolve dns for domain 1442468604548.com] field: 'email' } ]
[ { [Error: email: could not resolve dns for domain 1442469859410.com] field: 'email' } ]
```

#### type
Expand Down
14 changes: 14 additions & 0 deletions doc/example/multiple-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// validate a field as one of multiple types
var Schema = require('../..')
, descriptor = {
flag: {type: ['boolean', Boolean], required: true}
}
, source = {flag: 'foo'}
, schema;

require('../../plugin/all');

schema = new Schema(descriptor);
schema.validate(source, function(err, res) {
console.dir(res.errors);
});
28 changes: 28 additions & 0 deletions system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// schema for a schema definition
var schema = {
type: 'object',
required: true,
fields: {
all: {
match: /./,
fields: {
additional: {type: 'boolean'},
list: {type: 'array'},
fields: {type: ['object', 'array']},
format: {type: 'string'},
len: {type: 'integer'},
min: {type: 'integer'},
max: {type: 'integer'},
pattern: {type: 'regexp'},
placeholder: {type: 'function'},
required: {type: 'boolean'},
test: {type: 'function'},
type: {type: ['string', 'function'], required: true},
values: {type: ['object', 'array']},
whitespace: {type: 'boolean'}
}
}
}
}

module.exports = schema;

0 comments on commit c02c8c9

Please sign in to comment.