readme.md 3.36 KB
Newer Older
Jaap Karan Singh's avatar
Jaap Karan Singh committed
1
jsonschema EXTRAAA!!!      [![NPM version][npm-image]][npm-url]
Jaap Karan Singh's avatar
Jaap Karan Singh committed
2 3
=====================

Jaap Karan Singh's avatar
Jaap Karan Singh committed
4
**Extends jsonschema validator with some common custom types and attributes**
Jaap Karan Singh's avatar
Jaap Karan Singh committed
5 6


7 8 9
Usage
-----

Jaap Karan Singh's avatar
Jaap Karan Singh committed
10 11
### Installation

12
```bash
Jaap Karan Singh's avatar
Jaap Karan Singh committed
13
npm install jsonschema-extra --save
Jaap Karan Singh's avatar
Jaap Karan Singh committed
14 15
```

Jaap Karan Singh's avatar
Jaap Karan Singh committed
16 17
### Example

18
```js
Jaap Karan Singh's avatar
Jaap Karan Singh committed
19 20 21 22 23
var jsonschema = require('jsonschema');
var extend = require('jsonschema-extra');

var validator = new (jsonschema.Validator)();
extend(validator);
24 25 26 27 28 29 30

// regexp
validator.validate(/abc/, { type: 'regexp' });

// error
validator.validate(new Error(), { type: 'error' });

Jaap Karan Singh's avatar
Jaap Karan Singh committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// mongodb objectid
validator.validate('123456789012345678901234', { type: 'objectId' });
```


### Details

#### Attributes

See `jsonschema` documentation for more detailed documentation on custom attributes.

##### validate

Custom validator when `jsonschema` just isn't what you are after. This one is my absolute favourite! It comes in handy whenever you are trying to do something complex.

> One of the features of jsonschema is that you can share it across client and server which is exactly what I do! However, if your schema contains a validate attribute it will no longer be valid json. This is not a big issue if you are ok for client side code to not contain all your validation logic if schema is sent from the server. 

48 49
**Single validator**

Jaap Karan Singh's avatar
Jaap Karan Singh committed
50 51 52 53 54 55 56 57 58 59
```js
var schema = {
	validate: function(instance, schema, options) {
		// do your crazy validation here
			// return an error string if not valid
		return 'is not a valid instance';
	}
};
```

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
**Multiple validators**

```js
var schema = {
	validate: [
		function(instance, schema, options) {
			// do your crazy validation here
				// return an error string if not valid
			return 'is not a valid instance';
		},
		function(instance, schema, options) {
			// do your crazy validation here
				// return an error string if not valid
			return 'is not a valid instance';
		}
	]
};
```

Jaap Karan Singh's avatar
Jaap Karan Singh committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

#### Types

Supported types:

- error (instanceof Error)
- regexp (instanceof RegExp)
- function (Function - also works for Generator Function)
- generatorFunction (ES6 Generator Function)
- objectId (MongoDb objectId)
- plainObject (calls _.isPlainObject)

```js
validator.validate(new Error(), { type: 'error' });

validator.validate(/regexp/, { type: 'regexp' });

validator.validate(function() {}, { type: 'function' });

validator.validate(function *() {}, { type: 'generatorFunction' });

100
validator.validate('123456789012345678901234', { type: 'objectId' });
Jaap Karan Singh's avatar
Jaap Karan Singh committed
101 102 103
validator.validate(new ObjectID(), { type: 'objectId' });

validator.validate({ a: 'a', b: 'b' }, { type: 'plainObject' });
104 105 106
```


107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
Testing
-------

Install `mocha` globally

```bash
$ npm install mocha -g
```

Run tests

```bash
$ npm test
```


Jaap Karan Singh's avatar
Jaap Karan Singh committed
123 124 125
Changelog
---------

126 127 128
### v1.1.0 (18 Sep 2014)
- Allow multiple validators in the `validate` property

129 130 131 132
### v1.0.2 (30 Sep 2014)
- Fixed so it works on non-Windows systems
- Removed mongodb dev dependency

Jaap Karan Singh's avatar
Jaap Karan Singh committed
133 134 135 136
### v1.0.1 (27 Sep 2014)
- Updated docs
- Updated package.json

Jaap Karan Singh's avatar
Jaap Karan Singh committed
137
### v1.0.0 (27 Sep 2014)
Jaap Karan Singh's avatar
Jaap Karan Singh committed
138
- Changed public API - jsonschema is no longer a dependency. This lets jsonschema to be updated in userland without this module requiring an update.
Jaap Karan Singh's avatar
Jaap Karan Singh committed
139 140
- Name of module changed to jsonschema-extra
- Removed conditionalEnum custom attribute since this has been fixed in the latest versions of jsonschema
Jaap Karan Singh's avatar
Jaap Karan Singh committed
141
- Removed `speed` type
142

Jaap Karan Singh's avatar
Jaap Karan Singh committed
143

Jaap Karan Singh's avatar
Jaap Karan Singh committed
144 145
[npm-image]: https://img.shields.io/npm/v/jsonschema-extra.svg?style=flat-square
[npm-url]: https://npmjs.org/package/jsonschema-extra