From 2f0c850c0984915be881f9900e1ec2e1fc3b9e3a Mon Sep 17 00:00:00 2001 From: suchothendav Date: Mon, 24 Oct 2016 14:48:38 -0700 Subject: [PATCH 1/2] Extend the supported data types using `types` option --- README.md | 17 +++++++++++++++++ index.js | 4 ++++ test/misc.js | 13 +++++++++++++ 3 files changed, 34 insertions(+) diff --git a/README.md b/README.md index 104a425..916553c 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,23 @@ console.log(validate.errors) // [{field: 'data.y', message: 'is required'}, // {field: 'data.x', message: 'is the wrong type'}] ``` +## Extend the supported data types using `types` option + +Add additional types (or extend the default types) by specifying the `types` option. For example if you need to add a validator function for type `file` (Swagger has a data type file), define file validator function as an `option` to validator. + +```js +var schema = { type: 'file' } +var validate = validator(schema, { + types: { + file: function (filename) { + return 'typeof '+filename+' === "string"'; + } + } +}); +validate('somefile.txt'); + +``` + ## Performance is-my-json-valid uses code generation to turn your JSON schema into basic javascript code that is easily optimizeable by v8. diff --git a/index.js b/index.js index 779cfe2..22ae840 100644 --- a/index.js +++ b/index.js @@ -114,6 +114,10 @@ var toType = function(node) { } var compile = function(schema, cache, root, reporter, opts) { + //Extend the types + if (opts && opts.types) { + types = xtend(types, opts.types); + } var fmts = opts ? xtend(formats, opts.formats) : formats var scope = {unique:unique, formats:fmts, isMultipleOf:isMultipleOf} var verbose = opts ? !!opts.verbose : false; diff --git a/test/misc.js b/test/misc.js index 4ea36d5..2bf978d 100644 --- a/test/misc.js +++ b/test/misc.js @@ -469,3 +469,16 @@ tape('field shows item index in arrays', function(t) { t.strictEqual(validate.errors[0].field, 'data.1.1.foo', 'should output the field with specific index of failing item in the error') t.end() }) + +tape('Extend types', function(t) { + var schema = { type: 'file' } + var validate = validator(schema, { + types: { + file: function (filename) { + return 'typeof '+filename+' === "string"'; + } + } + }); + t.ok(validate('somefile.text'), 'is file') + t.end() +}) From 04e76001ddcf14d6bd0724e04ec86a14024be040 Mon Sep 17 00:00:00 2001 From: suchothendav Date: Mon, 12 Dec 2016 21:38:27 -0800 Subject: [PATCH 2/2] rebase with upstream --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 9d17d68..dba80d0 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,6 @@ console.log(validate.errors) // [{field: 'data.y', message: 'is required'}, // {field: 'data.x', message: 'is the wrong type'}] ``` -<<<<<<< HEAD ## Extend the supported data types using `types` option Add additional types (or extend the default types) by specifying the `types` option. For example if you need to add a validator function for type `file` (Swagger has a data type file), define file validator function as an `option` to validator. @@ -171,7 +170,7 @@ var validate = validator(schema, { validate('somefile.txt'); ``` -======= + ## Error messages Here is a list of possible `message` values for errors: @@ -198,7 +197,6 @@ Here is a list of possible `message` values for errors: * `has less length than allowed` * `is less than minimum` * `is more than maximum` ->>>>>>> 8015f4bc67fdc3f6aee162bfcfb0765e474d2344 ## Performance