feat: option processCode replaced option beautify, closes #382
parent
6036f3ff07
commit
fc1c46cbfa
10
README.md
10
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()`.
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue