|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +var fs = require('fs'); |
3 | 4 | var mapStream = require('map-stream'); |
4 | 5 | var colors = require('ansi-colors'); |
5 | 6 | var jsonlint = require('@prantlf/jsonlint'); |
| 7 | +var validator = require('@prantlf/jsonlint/lib/validator'); |
6 | 8 | var sorter = require('@prantlf/jsonlint/lib/sorter'); |
7 | 9 | var through = require('through2'); |
8 | 10 | var PluginError = require('plugin-error'); |
9 | 11 | var log = require('fancy-log'); |
10 | 12 |
|
11 | | -var formatOutput = function (msg) { |
12 | | - var output = {}; |
13 | | - |
14 | | - if (msg) { output.message = msg; } |
15 | | - |
16 | | - output.success = msg ? false : true; |
17 | | - |
18 | | - return output; |
19 | | -}; |
20 | | - |
21 | 13 | var jsonLintPlugin = function (options) { |
22 | 14 | options = Object.assign({ |
23 | 15 | mode: 'json', |
24 | 16 | ignoreComments: false, |
25 | 17 | ignoreTrailingCommas: false, |
26 | 18 | allowSingleQuotedStrings: false, |
27 | 19 | allowDuplicateObjectKeys: true, |
| 20 | + schema: {}, |
28 | 21 | format: false, |
29 | 22 | indent: 2, |
30 | 23 | sortKeys: false |
31 | 24 | }, options); |
| 25 | + var schema = options.schema; |
| 26 | + var parserOptions = { |
| 27 | + mode: options.mode, |
| 28 | + ignoreComments: options.ignoreComments || options.cjson || |
| 29 | + options.mode === 'cjson' || options.mode === 'json5', |
| 30 | + ignoreTrailingCommas: options.ignoreTrailingCommas || options.mode === 'json5', |
| 31 | + allowSingleQuotedStrings: options.allowSingleQuotedStrings || options.mode === 'json5', |
| 32 | + allowDuplicateObjectKeys: options.allowDuplicateObjectKeys, |
| 33 | + environment: schema.environment |
| 34 | + }; |
| 35 | + var schemaContent; |
| 36 | + |
| 37 | + function createResult (message) { |
| 38 | + var result = {}; |
| 39 | + if (message) { |
| 40 | + result.message = message; |
| 41 | + result.success = false; |
| 42 | + } else { |
| 43 | + result.success = true; |
| 44 | + } |
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + function formatOutput (parsedData, file) { |
| 49 | + if (options.format) { |
| 50 | + if (options.sortKeys) { |
| 51 | + parsedData = sorter.sortObject(parsedData); |
| 52 | + } |
| 53 | + var formatted = JSON.stringify(parsedData, null, options.indent) + '\n'; |
| 54 | + file.contents = new Buffer(formatted); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + function validateSchema (parsedData, file, finish) { |
| 59 | + var errorMessage; |
| 60 | + try { |
| 61 | + var validate = validator.compile(schemaContent, parserOptions); |
| 62 | + validate(parsedData); |
| 63 | + formatOutput(parsedData, file); |
| 64 | + } |
| 65 | + catch (error) { |
| 66 | + errorMessage = error.message; |
| 67 | + } |
| 68 | + finish(errorMessage); |
| 69 | + } |
| 70 | + |
| 71 | + function loadAndValidateSchema (parsedData, file, finish) { |
| 72 | + if (schemaContent) { |
| 73 | + validateSchema(parsedData, finish); |
| 74 | + } else { |
| 75 | + fs.readFile(schema.src, 'utf-8', function(error, fileContent) { |
| 76 | + if (error) { |
| 77 | + finish(error.message); |
| 78 | + } else { |
| 79 | + schemaContent = fileContent; |
| 80 | + validateSchema(parsedData, file, finish); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
32 | 85 |
|
33 | 86 | return mapStream(function (file, cb) { |
34 | | - var errorMessage = ''; |
35 | | - |
36 | | - var parserOptions = { |
37 | | - mode: options.mode, |
38 | | - ignoreComments: options.ignoreComments || options.cjson || |
39 | | - options.mode === 'cjson' || options.mode === 'json5', |
40 | | - ignoreTrailingCommas: options.ignoreTrailingCommas || options.mode === 'json5', |
41 | | - allowSingleQuotedStrings: options.allowSingleQuotedStrings || options.mode === 'json5', |
42 | | - allowDuplicateObjectKeys: options.allowDuplicateObjectKeys |
43 | | - }; |
| 87 | + var errorMessage; |
| 88 | + function finish (errorMessage) { |
| 89 | + file.jsonlint = createResult(errorMessage); |
| 90 | + cb(null, file); |
| 91 | + } |
| 92 | + |
44 | 93 | try { |
45 | 94 | var parsedData = jsonlint.parse(String(file.contents), parserOptions); |
46 | | - if (options.format) { |
47 | | - if (options.sortKeys) { |
48 | | - parsedData = sorter.sortObject(parsedData); |
49 | | - } |
50 | | - var formatted = JSON.stringify(parsedData, null, options.indent) + '\n'; |
51 | | - file.contents = new Buffer(formatted); |
| 95 | + if (schema.src) { |
| 96 | + loadAndValidateSchema(parsedData, file, finish); |
| 97 | + return; |
52 | 98 | } |
| 99 | + formatOutput(parsedData, file); |
53 | 100 | } |
54 | | - catch (err) { |
55 | | - errorMessage = err.message; |
| 101 | + catch (error) { |
| 102 | + errorMessage = error.message; |
56 | 103 | } |
57 | | - file.jsonlint = formatOutput(errorMessage); |
58 | | - |
59 | | - cb(null, file); |
| 104 | + finish(errorMessage); |
60 | 105 | }); |
61 | 106 | }; |
62 | 107 |
|
|
0 commit comments