问题
I am trying to express objects and relationships between them. Every object has an ID and every relationship references 2 object ids. I'd like to make sure every relationship references existing object ids. Could you do this with Json Schema?
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"Objects": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
}
}
}
},
"Relations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"objId1": {"type": "integer"}, // I'd like these
"objId2": {"type": "integer"} // Two fields to reference an existing Objects.id
}
}
},
},
}
回答1:
No you cannot do this with JSON Schema. Sorry.
It sounds like this is a database. You should consider using database level constraints for this sort of validation.
回答2:
Within-document referential integrity constraints of the type required here are supported in JESS (JSON Extended Structural Schemas). See in particular https://bitbucket.org/pkoppstein/jess/wiki/Home#markdown-header-specifying-within-document-referential-integrity-constraints
Incidentally, between-document referential integrity constraints can also be handled, if somewhat clunkily, by combining the separate documents into a composite document.
Disclaimer: I am the author of the JESS specification and validation module.
来源:https://stackoverflow.com/questions/56429622/make-sure-object-field-has-value-existing-in-another-field-in-json-schema