8000 Custom dynamicDefaults functions don't work with default parameters and no args given in schema · Issue #139 · ajv-validator/ajv-keywords · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Custom dynamicDefaults functions don't work with default parameters and no args given in schema #139
Open
@ottomata

Description

@ottomata

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0