From fc1c46cbfafece7f0d65b28c52c965ab0903d2db Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 26 Dec 2016 16:57:06 +0000 Subject: [PATCH] feat: option processCode replaced option beautify, closes #382 --- README.md | 10 +++++----- lib/ajv.d.ts | 5 +++-- lib/ajv.js | 1 - lib/compile/index.js | 17 +---------------- spec/ajv_options.js | 3 ++- spec/issues.spec.js | 2 +- spec/options.spec.js | 16 ++++++++++++++++ 7 files changed, 28 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 9adf95e..6408be2 100644 --- a/README.md +++ b/README.md @@ -971,7 +971,7 @@ Defaults: // referenced schema options: missingRefs: true, extendRefs: 'fail', - loadSchema: undefined, // function(uri) {}, it should return Promise + loadSchema: undefined, // function(uri: string): Promise {} // options to modify validated data: removeAdditional: false, useDefaults: false, @@ -989,9 +989,9 @@ Defaults: ownProperties: false, multipleOfPrecision: false, errorDataPath: 'object', - sourceCode: false, messages: true, - beautify: false, + sourceCode: false, + processCode: undefined, // function (str: string): string {} cache: new Cache } ``` @@ -1082,9 +1082,9 @@ Defaults: - _ownProperties_: by default Ajv iterates over all enumerable object properties; when this option is `true` only own enumerable object properties (i.e. found directly on the object rather than on its prototype) are iterated. Contributed by @mbroadst. - _multipleOfPrecision_: by default `multipleOf` keyword is validated by comparing the result of division with parseInt() of that result. It works for dividers that are bigger than 1. For small dividers such as 0.01 the result of the division is usually not integer (even when it should be integer, see issue [#84](https://github.com/epoberezkin/ajv/issues/84)). If you need to use fractional dividers set this option to some positive integer N to have `multipleOf` validated using this formula: `Math.abs(Math.round(division) - division) < 1e-N` (it is slower but allows for float arithmetics deviations). - _errorDataPath_: set `dataPath` to point to 'object' (default) or to 'property' when validating keywords `required`, `additionalProperties` and `dependencies`. -- _sourceCode_: add `sourceCode` property to validating function (for debugging; this code can be different from the result of toString call). - _messages_: Include human-readable messages in errors. `true` by default. `false` can be passed when custom messages are used (e.g. with [ajv-i18n](https://github.com/epoberezkin/ajv-i18n)). -- _beautify_: format the generated function with [js-beautify](https://github.com/beautify-web/js-beautify) (the validating function is generated without line-breaks). `npm install js-beautify` to use this option. `true` or js-beautify options can be passed. +- _sourceCode_: add `sourceCode` property to validating function (for debugging; this code can be different from the result of toString call). +- _processCode_: an optional function to process generated code before it is passed to Function constructor (in case of async schema it is called before transpilation). Starting from version 5.0.0 this option replaced the option `beautify` that formatted the generated function using [js-beautify](https://github.com/beautify-web/js-beautify) (the validating function is generated without line-breaks). If you want to beautify the generated code pass `require('js-beautify').js_beautify`. - _cache_: an optional instance of cache to store compiled schemas using stable-stringified schema as a key. For example, set-associative cache [sacjs](https://github.com/epoberezkin/sacjs) can be used. If not passed then a simple hash is used which is good enough for the common use case (a limited number of statically defined schemas). Cache should have methods `put(key, value)`, `get(key)`, `del(key)` and `clear()`. diff --git a/lib/ajv.d.ts b/lib/ajv.d.ts index bb6d78d..11274e8 100644 --- a/lib/ajv.d.ts +++ b/lib/ajv.d.ts @@ -115,7 +115,7 @@ declare namespace ajv { } interface Options { - v5?: boolean; + $data?: boolean; allErrors?: boolean; verbose?: boolean; jsonPointers?: boolean; @@ -143,7 +143,8 @@ declare namespace ajv { multipleOfPrecision?: boolean | number; errorDataPath?: string, messages?: boolean; - beautify?: boolean | Object; + sourceCode?: boolean; + processCode?: (code: string) => string; cache?: Object; } diff --git a/lib/ajv.js b/lib/ajv.js index 0a4061b..59891ce 100644 --- a/lib/ajv.js +++ b/lib/ajv.js @@ -75,7 +75,6 @@ function Ajv(opts) { opts.loopRequired = opts.loopRequired || Infinity; if (opts.async || opts.transpile) async.setup(opts); - if (opts.beautify === true) opts.beautify = { indent_size: 2 }; if (opts.errorDataPath == 'property') opts._errorDataPathProperty = true; this._metaOpts = getMetaSchemaOptions(this); diff --git a/lib/compile/index.js b/lib/compile/index.js index 98faedb..adcc22d 100644 --- a/lib/compile/index.js +++ b/lib/compile/index.js @@ -6,16 +6,6 @@ var resolve = require('./resolve') , stableStringify = require('json-stable-stringify') , async = require('../async'); -var beautify; - -function loadBeautify(){ - if (beautify === undefined) { - var name = 'js-beautify'; - try { beautify = require(name).js_beautify; } - catch(e) { beautify = false; } - } -} - var validateGenerator = require('../dotjs/validate'); /** @@ -123,12 +113,7 @@ function compile(schema, root, localRefs, baseId) { + vars(defaults, defaultCode) + vars(customRules, customRuleCode) + sourceCode; - if (opts.beautify) { - loadBeautify(); - /* istanbul ignore else */ - if (beautify) sourceCode = beautify(sourceCode, opts.beautify); - else console.error('"npm install js-beautify" to use beautify option'); - } + if (opts.processCode) sourceCode = opts.processCode(sourceCode); // console.log('\n\n\n *** \n', sourceCode); var validate, validateCode , transpile = opts._transpileFunc; diff --git a/spec/ajv_options.js b/spec/ajv_options.js index c798656..e09e73b 100644 --- a/spec/ajv_options.js +++ b/spec/ajv_options.js @@ -14,6 +14,7 @@ var options = fullTest } : { allErrors: true }; -if (fullTest && !isBrowser) options.beautify = true; +if (fullTest && !isBrowser) + options.processCode = require('js-beautify').js_beautify; module.exports = options; diff --git a/spec/issues.spec.js b/spec/issues.spec.js index fd5c495..d520c85 100644 --- a/spec/issues.spec.js +++ b/spec/issues.spec.js @@ -156,7 +156,7 @@ describe('issue #181, custom keyword is not validated in allErrors mode if there }); function testCustomKeywordErrors(def) { - var ajv = new Ajv({ allErrors: true, beautify: true }); + var ajv = new Ajv({ allErrors: true }); ajv.addKeyword('alwaysFails', def); diff --git a/spec/options.spec.js b/spec/options.spec.js index 09e385a..9127f04 100644 --- a/spec/options.spec.js +++ b/spec/options.spec.js @@ -941,4 +941,20 @@ describe('Ajv Options', function () { }); }); }); + + + describe('processCode', function() { + it('should process generated code', function() { + var ajv = new Ajv; + var validate = ajv.compile({type: 'string'}); + validate.toString().split('\n').length .should.equal(1); + + var beautify = require('js-beautify').js_beautify; + var ajvPC = new Ajv({processCode: beautify}); + validate = ajvPC.compile({type: 'string'}); + validate.toString().split('\n').length .should.be.above(1); + validate('foo') .should.equal(true); + validate(1) .should.equal(false); + }); + }); });