ajv-keywords icon indicating copy to clipboard operation
ajv-keywords copied to clipboard

Custom dynamicDefaults functions don't work with default parameters and no args given in schema

Open ottomata opened this issue 5 years ago • 0 comments
trafficstars

The examples of how to add a custom uuid function dynamicDefaults wrap uuid.v4(). However, I'd expect to be able to use either just uuid (which calls uuid.v4) or uuid.v4 itself, without a function uuidv4() wrapper.

It seems that the code in dynamicDefaults uses `func.length figure out if the value of the custom dynamicDefault should be called with arguments.

funcs[key] = func.length ? func(d.args) : func;

(I didn't know this, but In javascript, Function.length is the number of arguments.)

> uuid = require('uuid');
{ [Function: v4] v1: [Function: v1], v4: [Circular] }
> uuid.length
3

I guess func.length is being used to figure out if arguments should be passed to the func as defined in the schema via {func: "uuid", args }, but perhaps a better way would be to just check if args is defined?

It'd be nice if I didn't have to make a wrapper for functions that have default parameters.

ottomata avatar Oct 29 '20 19:10 ottomata