source: Dev/trunk/node_modules/grunt-tv4/tasks/tv4.js @ 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: 3.3 KB
Line 
1/*
2 * grunt-tv4
3 * https://github.com/Bartvds/grunt-tv4
4 *
5 * Copyright (c) 2013 Bart van der Schoor
6 * Licensed under the MIT license.
7 */
8
9'use strict';
10
11var taskTv4 = require('tv4').freshApi();
12var reporter = require('tv4-reporter');
13var ministyle = require('ministyle');
14var miniwrite = require('miniwrite');
15var loader = require('../lib/loader');
16var runnerModule = require('../lib/runner');
17
18//var util = require('util');
19
20module.exports = function (grunt) {
21
22        var out = miniwrite.grunt(grunt);
23        var style = ministyle.grunt();
24        var report = reporter.getReporter(out, style);
25        var runner = runnerModule.getRunner(taskTv4, loader.getLoaders(), out, style);
26
27        grunt.registerMultiTask('tv4', 'Validate values against json-schema v4.', function () {
28                var done = this.async();
29
30                //import options
31                var context = runner.getContext(this.options(runner.getOptions({
32                        timeout: 5000
33                })));
34                var objects = [];
35
36                if (context.options.fresh) {
37                        context.tv4 = taskTv4.freshApi();
38                }
39                else {
40                        context.tv4 = taskTv4;
41                }
42
43                grunt.util._.each(context.options.schemas, function (schema, uri) {
44                        context.tv4.addSchema(uri, schema);
45                });
46
47                context.tv4.addFormat(context.options.formats);
48
49                grunt.util._.each(context.options.languages, function (language, id) {
50                        context.tv4.addLanguage(id, language);
51                });
52                if (context.options.language) {
53                        context.tv4.language(context.options.language);
54                }
55
56                //flatten list for sanity
57                grunt.util._.each(this.files, function (f) {
58                        grunt.util._.some(f.src, function (filePath) {
59                                if (!grunt.file.exists(filePath)) {
60                                        grunt.log.warn('file "' + filePath + '" not found.');
61                                        return true;
62                                }
63                                objects.push({
64                                        path: filePath,
65                                        label: filePath,
66                                        root: context.options.root
67                                });
68                        });
69                });
70
71                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72
73                var values = this.data.values;
74                if (typeof values === 'function') {
75                        values = values();
76                }
77
78                if (typeof values === 'object') {
79                        var keyPrefix = (Array.isArray(values) ? 'value #' : '');
80                        grunt.util._.some(values, function (value, key) {
81                                objects.push({
82                                        label: keyPrefix + key,
83                                        value: value,
84                                        root: context.options.root
85                                });
86                        });
87                }
88                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
89
90                if (context.options.add && Array.isArray(context.options.add)) {
91                        grunt.util._.some(context.options.add, function (schema) {
92                                if (typeof schema === 'string') {
93                                        //juggle
94                                        schema = grunt.file.readJSON(schema);
95                                }
96                                if (typeof schema.id === 'undefined') {
97                                        grunt.log.warn('options.add: schema missing required id field (use options.schema to map it manually)');
98                                        grunt.log.writeln();
99                                        context.done(false);
100                                        return true;
101                                }
102                                context.tv4.addSchema(schema.id, schema);
103                        });
104                }
105
106                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107
108                //grunt.verbose.writeln(util.inspect(context));
109
110                context.validate(objects, function (err, job) {
111                        if (err) {
112                                throw err;
113                        }
114                        if (job) {
115                                report.reportBulk(job.failed, job.passed);
116                                if (!job.success) {
117                                        grunt.log.writeln('');
118                                }
119                                done(job.success);
120                        }
121                        else {
122                                done(false);
123                        }
124                });
125        });
126};
Note: See TracBrowser for help on using the repository browser.