Package Exports
- @prantlf/gulp-jsonlint
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@prantlf/gulp-jsonlint) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@prantlf/gulp-jsonlint
JSON/JSON5 file syntax validation plugin for
gulp
usingjsonlint
This is a fork of the original package with the following enhancements:
- Validates with both JSON and JSON5 standards.
- Optionally recognizes JavaScript-style comments and single quoted strings.
- Optionally ignores trailing commas and reports duplicate object keys as an error.
- Prefers using the 8x faster native JSON parser, if possible.
- Depends on up-to-date npm modules with no installation warnings.
Usage
First, install @prantlf/gulp-jsonlint
as a development dependency:
npm i -D @prantlf/gulp-jsonlint
Then, add it to your gulpfile.js
:
var jsonlint = require("gulp-jsonlint");
gulp.src("./src/*.json")
.pipe(jsonlint())
.pipe(jsonlint.reporter());
Using a custom reporter:
var jsonlint = require('gulp-jsonlint');
var log = require('fancy-log');
var myCustomReporter = function (file) {
log('File ' + file.path + ' is not valid JSON.');
};
gulp.src('./src/*.json')
.pipe(jsonlint())
.pipe(jsonlint.reporter(myCustomReporter));
API
jsonlint(options)
Options can be passed as keys in an object to the jsonlint
function. The following are their defaults:
jsonlint({
mode: 'json',
ignoreComments: false,
ignoreTrailingCommas: false,
allowSingleQuotedStrings: false,
allowDuplicateObjectKeys: true,
})
mode
, when set to "cjson" or "json5", enables some other flags automaticallyignoreComments
, whentrue
JavaScript-style single-line and multiple-line comments will be recognised and ignoredignoreTrailingCommas
, whentrue
trailing commas in objects and arrays will be ignoredallowSingleQuotedStrings
, whentrue
single quotes will be accepted as alternative delimiters for stringsallowDuplicateObjectKeys
, whenfalse
duplicate keys in objects will be reported as an error
jsonlint.reporter(customReporter)
customReporter(file)
Type: function
You can pass a custom reporter function. If ommited then the default reporter will be used.
The customReporter
function will be called with the argument file
.
file
Type: object
This argument has the attribute jsonlint
wich is an object that contains a success
boolean attribute. If it's false you also have a message
attribute containing the jsonlint error message.
jsonlint.failOnError()
Stop a task/stream if an jsonlint error has been reported for any file.
// Cause the stream to stop(/fail) before copying an invalid JS file to the output directory
gulp.src('**/*.js')
.pipe(jsonlint())
.pipe(jsonlint.failOnError())
.pipe(gulp.dest('../output'));
jsonlint.failAfterError()
Stop a task/stream if an jsonlint error has been reported for any file, but wait for all of them to be processed first.
License
Copyright (C) 2013-2019 Rogério Vicente, Ferdinand Prantl
Licensed under the MIT License.