From 3e9cd16d2ce697bea83d996af97378e5f4c8fe2b Mon Sep 17 00:00:00 2001 From: David Worms Date: Fri, 28 Dec 2018 23:22:44 +0100 Subject: [PATCH] relax: validation refinements --- CHANGELOG.md | 1 + lib/es5/index.js | 11 +++++++++-- lib/index.js | 9 ++++++++- test/option.relax.coffee | 12 ++++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b30627..b269ccc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Enhancements: +* relax: validation refinements * delimiter: validation refinements * max_record_size: validation refinements diff --git a/lib/es5/index.js b/lib/es5/index.js index 38d2f6e..5164625 100644 --- a/lib/es5/index.js +++ b/lib/es5/index.js @@ -55,7 +55,6 @@ var default_options = { from_line: 1, objname: undefined, // TODO create a max_comment_size - relax: false, relax_column_count: false, skip_empty_lines: false, skip_lines_with_empty_values: false, @@ -237,7 +236,15 @@ function (_Transform) { } return rd; - }); // Normalize options `trim`, `ltrim` and `rtrim` + }); // Normalize option `relax` + + if (typeof options.relax === 'boolean') {// Great, nothing to do + } else if (options.relax === undefined || options.relax === null) { + options.relax = false; + } else { + throw new Error("Invalid Option: relax must be a boolean, got ".concat(JSON.stringify(options.relax))); + } // Normalize options `trim`, `ltrim` and `rtrim` + if (options.trim === true && options.ltrim !== false) { options.ltrim = true; diff --git a/lib/index.js b/lib/index.js index f970a78..c7b9e2b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -15,7 +15,6 @@ const default_options = { from_line: 1, objname: undefined, // TODO create a max_comment_size - relax: false, relax_column_count: false, skip_empty_lines: false, skip_lines_with_empty_values: false, @@ -168,6 +167,14 @@ class Parser extends Transform { } return rd }) + // Normalize option `relax` + if(typeof options.relax === 'boolean'){ + // Great, nothing to do + }else if(options.relax === undefined || options.relax === null){ + options.relax = false + }else{ + throw new Error(`Invalid Option: relax must be a boolean, got ${JSON.stringify(options.relax)}`) + } // Normalize options `trim`, `ltrim` and `rtrim` if(options.trim === true && options.ltrim !== false){ options.ltrim = true diff --git a/test/option.relax.coffee b/test/option.relax.coffee index c06e5b4..a010eb5 100644 --- a/test/option.relax.coffee +++ b/test/option.relax.coffee @@ -2,6 +2,18 @@ parse = require '../lib' describe 'Option `relax`', -> + + it 'validation', -> + parse '', relax: true, (->) + parse '', relax: false, (->) + parse '', relax: null, (->) + parse '', relax: undefined, (->) + (-> + parse '', relax: 1, (->) + ).should.throw 'Invalid Option: relax must be a boolean, got 1' + (-> + parse '', relax: 'oh no', (->) + ).should.throw 'Invalid Option: relax must be a boolean, got "oh no"' it 'true with invalid quotes in the middle', (next) -> # try with relax true