source: Dev/trunk/node_modules/grunt-tv4/README.md @ 532

Last change on this file since 532 was 487, checked in by hendrikvanantwerpen, 11 years ago

Completed migration to API, without CouchDB proxy.

Move to API is now completed. The full API is password protected, a very
limited API is exposed for respondents, which works with secrets that
are passed in URLs.

Serverside the HTTPResult class was introduced, which is similar to
Promises, but specifically for HTTP. It carries a status code and
response and makes it easier to extract parts of async handling in
separate functions.

Fixed a bug in our schema (it seems optional attributes don't exist but
a required list does). Verification of our schema by grunt-tv4 didn't
work yet. Our schema is organized the wrong way (this is fixable),
but the json-schema schema has problems with simple types and $refs.

File size: 6.2 KB
RevLine 
[487]1# grunt-tv4
2
3[![Build Status](https://secure.travis-ci.org/Bartvds/grunt-tv4.png?branch=master)](http://travis-ci.org/Bartvds/grunt-tv4) [![Dependency Status](https://gemnasium.com/Bartvds/grunt-tv4.png)](https://gemnasium.com/Bartvds/grunt-tv4) [![NPM version](https://badge.fury.io/js/grunt-tv4.png)](http://badge.fury.io/js/grunt-tv4)
4
5> Use grunt and [Tiny Validator tv4](https://github.com/geraintluff/tv4) to validate values against [json-schema](http://json-schema.org/) draft v4
6
7## Getting Started
8
9This plugin requires Grunt `~0.4.1`
10
11If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
12
13```shell
14$ npm install grunt-tv4 --save-dev
15```
16
17Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
18
19```js
20grunt.loadNpmTasks('grunt-tv4');
21```
22
23## The "tv4" task
24
25### Notes
26
27* Uses [Tiny Validator tv4 ](https://github.com/geraintluff/tv4) so schemas must conform to [json-schema draft v4](http://json-schema.org/documentation.html).
28* Supports automatically resolution and loading remote references by http via `$ref`.
29* To use `$ref` see the [json-schema](http://json-schema.org/) documentation or [this help](http://spacetelescope.github.io/understanding-json-schema/structuring.html).
30* For general help with json-schema see this excelent [guide](http://spacetelescope.github.io/understanding-json-schema/) and usable [reference](http://spacetelescope.github.io/understanding-json-schema/reference/index.html).
31* Errors formatted by the [tv4-reporter](https://github.com/Bartvds/tv4-reporter) library.
32
33### API change
34
35As of version `v0.2.0` the API was changed to follow the Grunt options- and file-selection conventions. The old pattern (which abused the destination-specifier) is no longer supported. The readme for the previous API can be found [here](https://github.com/Bartvds/grunt-tv4/tree/71ef1726945d05efd5daca29f26cbf4ab09c858e).
36
37The root schema must now to be specified as `options.root`.
38
39### Example
40
41* Demo of version `v0.3.0` output on [travis-ci](https://travis-ci.org/Bartvds/grunt-tv4/jobs/14468920)
42
43### Basic usage
44
45Validate from .json files:
46
47```js
48grunt.initConfig({
49        tv4: {
50                options: {
51                    root: grunt.file.readJSON('schema/main.json')
52                }
53                myTarget: {
54                        src: ['data/*.json']
55                }
56        }
57})
58```
59
60Valdiate values:
61
62```js
63grunt.initConfig({
64        tv4: {
65                myTarget: {
66                        options: {
67                                root: {
68                                        type: 'object',
69                                        properties: { ... }
70                                }
71                        },
72                        values: [
73                                { ... },
74                                { ... }
75                        ]
76                }
77        }
78})
79````
80
81### Advanced usage
82
83```js
84grunt.initConfig({
85        tv4: {
86                options: {
87                        // specify the main schema, one of:
88            // - path to json
89            // - http-url
90            // - schema object
91            // - callback that returns one of the above
92                        root: grunt.file.readJSON('schema/main.json'),
93
94                        // show multiple errors per file (off by default)
95                        multi: true,
96
97                        // create a new tv4 instance for every target (off by default)
98                        fresh: true,
99
100                        // add schemas in bulk (each required to have an 'id' property) (can be a callback)
101                        add: [
102                                 grunt.file.readJSON('schema/apple.json'),
103                                 grunt.file.readJSON('schema/pear.json')
104                        ],
105
106                        // set schemas by URI (can be a callback)
107                        schemas: {
108                                'http://example.com/schema/apple': grunt.file.readJSON('schema/apple.json'),
109                                'http://example.com/schema/pear': grunt.file.readJSON('schema/pear.json')
110                        },
111
112                        // map of custom formats passed to tv4.addFormat()
113                        formats: {
114                                date: function (data, schema) {
115                                        if (typeof data !== 'string' || !dateRegex.test(data)) {
116                                                return 'value must be string of the form: YYYY-MM-DD';
117                                        }
118                                        return null;
119                                }
120                        },
121
122                        // passed to tv4.validate()
123                        checkRecursive: false
124                        // passed to tv4.validate()
125                        banUnknownProperties: false
126                        // passed tv4.language()
127                        language: 'de'
128                        // passed tv4.addLanguage()
129                        languages: {
130                                'de': { ... }
131                        }
132                },
133                // load json from disk
134                myFiles: {
135                        src: ['data/*.json', 'data/fruit/**/*.json']
136                },
137
138                myValues: {
139                        // validate values
140                        values: [
141                                grunt.file.readJSON('data/apple.json'),
142                                grunt.file.readJSON('data/pear.json')
143                        ],
144                },
145
146                myValueMap: {
147                        // alternately pass as object and the keys will be used as labels in the reports
148                        values: {
149                                'apple': grunt.file.readJSON('data/apple.json'),
150                                'pear': grunt.file.readJSON('data/pear.json')
151                        },
152                },
153
154                myCallback: {
155                        // alternately pass a function() to return a collection of values (array or object)
156                        values: function() {
157                                return {
158                                        'apple': grunt.file.readJSON('data/apple.json'),
159                                        'pear': grunt.file.readJSON('data/pear.json')
160                                }
161                        }
162                }
163        }
164})
165```
166
167## History
168
169* 0.4.0 - Updated some depedencies. `root`, `add` and `schemas` can be a callback function (for lazy init).
170* 0.3.0 - Big internal rewrite:
171        * Added `.values` option.
172        * Extracted reporting to [tv4-reporter](https://github.com/Bartvds/tv4-reporter), [miniwrite](https://github.com/Bartvds/miniwrite) and [ministyle](https://github.com/Bartvds/ministyle).
173        * Moved loader logic to own stand-alone module (for later extraction)
174        * Extracted test-running logic to own module (for later extraction)
175* 0.2.1 - Added support to report subErrors (for anyOf/oneOf)
176* 0.2.0 - Updated to follow the Grunt conventions.
177* 0.1.4 - Updated `tv4` to version `1.0.11`
178  * Added support for `tv4.addFormat()` / `languages` / `checkRecursive` / `banUnknownProperties`.
179* 0.1.3 - Support for loading remote references (JSON Schema's `$ref`).
180* 0.1.1 - Bugfixes and improved reporting
181* 0.1.0 - First release with synchronous validation
182
183
184## Contributing
185In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
186
187
188[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/Bartvds/grunt-tv4/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
189
Note: See TracBrowser for help on using the repository browser.