ajv-validator / ajv-validator/ajv-keywords

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

Đang mở
#139 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
TypeScript
Star
257
Fork
51
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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](https://github.com/ajv-validator/ajv-keywords/blob/master/keywords/dynamicDefaults.js#L30) uses `func.length figure out if the value of the custom dynamicDefault should be called with arguments.

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

(I didn't know this, but In javascript, [Function.length is the number of arguments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length).)

```javascript
> 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.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.