ajv-validator / ajv-validator/ajv

ajv-keywords typeof function and default

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

Mô tả

**What version of Ajv are you using? Does the issue happen if you use the latest version?**

6.12.2

**Ajv options object**

```javascript
{ useDefaults: true }
```

**JSON Schema**

```js
{
"type": "object",
"properties": {
"foo": { "typeof": "function", "default": () => {} }
}
}
```

**Sample data**

See below.

**Your code**

https://runkit.com/satazor/5eac34960c2691001afd90ab

**Validation result, data AFTER validation, error messages**

Throws:

> should pass \"typeof\" keyword validation

**What results did you expect?**

Because the `foo` prop has a default set to `() => {}`, it should not throw.
The **issue** is that `ajv` is doing `JSON.stringify` on the default value, to clone values. However, that should not be done for functions, and possible other types.

~~Using `{ useDefaults: 'shared' }` fixes the issue, but it does introduce other problematic side-effects.~~ Actually, there's a missing `function` entry here: https://github.com/epoberezkin/ajv/blob/3b735371e74bf847146e07b59172a6d4485cc44a/lib/compile/index.js#L238

**Are you going to resolve the issue?**

I may try.

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

Mở hướng dẫn đóng góp

Đá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.