z-schema icon indicating copy to clipboard operation
z-schema copied to clipboard

breakOnFirstError does not work in CLI

Open itslenny opened this issue 8 years ago • 1 comments

I'm trying to disable break on first error using the cli z-schema command

Everything works fine with or without this flag, but when I try to set it to false it throws errors because it's trying to open false as a json file.

I tried both of these options:

z-schema --breakOnFirstError false ./some-schema.json ./some-data.json
z-schema --breakOnFirstError=false ./some-schema.json ./some-data.json

both resulted in this error:

{ Error: ENOENT: no such file or directory, open 'false'
    at Error (native)
    at Object.fs.openSync (fs.js:640:18)
    at Object.fs.readFileSync (fs.js:508:33)
    at readJson (.../AppData/Roaming/npm/node_modules/z-schema/bin/z-schema:54:18)
    at Object.<anonymous> (.../AppData/Roaming/npm/node_modules/z-schema/bin/z-schema:70:14)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3) errno: -2, code: 'ENOENT', syscall: 'open', path: 'false' }
.../AppData/Roaming/npm/node_modules/z-schema/bin/z-schema:57
        throw new Error("Couldn't read the file: " + fileName);
        ^

Error: Couldn't read the file: false
    at readJson (.../AppData/Roaming/npm/node_modules/z-schema/bin/z-schema:57:15)
    at Object.<anonymous> (.../AppData/Roaming/npm/node_modules/z-schema/bin/z-schema:70:14)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)

itslenny avatar Apr 25 '17 00:04 itslenny

I found a fix for it. I have it working locally. Not sure if I'll have time to do a PR or not, but here's the solution for reference:

in bin/z-schema

Line 33. Add the input <b> and a function to parse the string input into a boolean.

.option("--breakOnFirstError <b>", "stops validation as soon as an error is found, true by default but can be turned off", parseBoolean)

Line 42. explicitly check for an undefined type because false is obviously falsey but a valid input for this

if (typeof key !== 'undefined') {

Boolean parser (put this anywhere)

function parseBoolean(value) {
    return value && value.toLowerCase() !== 'true';
}

itslenny avatar Apr 25 '17 00:04 itslenny