Get all properties under a schema
rajsenthil opened this issue · comments
Senthil Rajendran commented
I have json schema as given below
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://example.com/product.schema.json",
"title": "Demo",
"description": "Demo properties and its definition",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"nested-prop": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "A unique id and cane be used in UI forms for an 'id' attribute "
},
"name": {
"type": "string",
"description": "handler name. This name can be used in UI form for 'name' attribute"
},
"label": {
"type": "string",
"description": "Label is used for displaying purpose. It is visible for example in UI forms"
},
"target": {
"type": "string",
"description": "'target' is a target service that validates and take actions"
}
},
"required": ["id", "name", "label", "target"]
}
},
"required": ["nested-prop"]
}
Is there a way, to get all the properties under nested-prop
in some way ["id", "name", "label", "target"]?
Please let me know
Kristian Mandrup commented
Try the new propKeys
getter method and the keyValye(key)
method directly available on the YupBuilder
instance.
I've also added a propKeysFor(key)
helper method which combines the two (available on master
)
propKeysFor(key) {
const keyPropsObj = keyValue(key);
return Object.keys(keyPropsObj);
}
Sample use
const builder = new YupBuilder(schema, config)
const props = builder.propKeysFor('nested-prop');
// alternative way to get the same
const propsObj = builder.keyValueFor('nested-prop');
const propsNestedProp = Object.keys(propsObj);
const { yupSchema } = builder;
const valid = yupSchema.isValidSync(testValueObj);