ajv-validator / ajv-validator/ajv
How can I get all properties that apply to a given object?
- 主要語言
- TypeScript
- 星號
- 14.8k
- 分支
- 1k
- PR 合併指標
- 30 天內沒有已合併 PR
描述
Given a schema and an object to validate, instead of (or in addition to) validating, I want to get all properties that apply to (or are valid for) the given object (i.e. all fixed properties and all conditional properties).
Can this be done?
For example, given the following data I want to get the following properties:
street_address, country, and the postal_code with the pattern "^\\d{5}$|^\\d{5}-\\d{4}$".
```
const schema = {
type: "object",
properties: {
street_address: {
type: "string",
},
country: {
enum: ["United States of America", "Canada", "Netherlands"],
},
},
allOf: [
{
if: {
properties: { country: { const: "United States of America" } },
},
then: {
properties: {
postal_code: { type: "string", pattern: "^\\d{5}$|^\\d{5}-\\d{4}$" },
},
},
},
{
if: {
properties: { country: { const: "Canada" } },
},
then: {
properties: {
postal_code: {
type: "string",
pattern: "[A-Z]\\d[A-Z] \\d[A-Z]\\d",
},
},
},
},
{
if: {
properties: { country: { const: "Netherlands" } },
},
then: {
properties: {
postal_code: { type: "string", pattern: "\\d{4} [A-Z]{2}" },
},
},
},
],
};
const data = {
country: "United States of America",
postal_code: "12345-999",
};
```
貢獻指南
評估
這個 Issue 還沒有評估資料。