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 |
|
---|
11 | var taskTv4 = require('tv4').freshApi();
|
---|
12 | var reporter = require('tv4-reporter');
|
---|
13 | var ministyle = require('ministyle');
|
---|
14 | var miniwrite = require('miniwrite');
|
---|
15 | var loader = require('../lib/loader');
|
---|
16 | var runnerModule = require('../lib/runner');
|
---|
17 |
|
---|
18 | //var util = require('util');
|
---|
19 |
|
---|
20 | module.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 | };
|
---|