Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Validate All Object Properties with JSON Schema
Post
Validate All Object Properties with JSON Schema
I'm writing a JSON schema to validate asset files for a program. The JSON I need to parse is structured so:
{
"jobs": {
"software-developer": { "job-description": "sw-dev.md:0", "pay": 80000 },
"sales-bro": { "job-description": "sales-bro", "pay": 190000 }
},
"people": {
"alice": { "name": "Alice", "profile-pic": "profile-alice.jpg:0" },
"bob": { "name": "Bob", "profile-pic": "bob.png:0" }
}
}
As you can see, each sub-object of a primary group follows a very deterministic pattern, but the top-level object keys vary based on the id of the asset being described. In this format, things like alice, bob, and sales-bro aren't being treated as object properties, but more like identifiers.
Is there a way in JSON schema to validate such a scheme? Can I create a rule that validates the values corresponding to every key within an object?
{
"$schema": "https://json-schema.org/draft-04/schema",
"$id": "https://example.com/my-assets.json",
"title": "Assets",
"type": "object",
"properties": {
"jobs": {
"type": "object",
"each-property": { // <--- dubious
"type": "object",
"properties": {
"job-description": ...,
"pay": ...
}
}
},
"people": {
// etc...
}
}
}
1 comment thread