Reputation: 91
We have similar kind of requirement where we want to validate the value of an attribute depending on the value of another attribute in the document. For example, we want to validate if the type is Home Phone then the value should be validated against a regular expression and similarly if the type is an email address then its value should be validated for a valid email.
"mediums" : [
{
"type" : "Home Phone",
"value" : "509 876 5432",
"allowContact" : true
},
{
"type" : "Email",
"value" : "[email protected]",
"allowContact" : false
}
]
Upvotes: 0
Views: 692
Reputation: 1915
If you want to use tools outside of MongoDB, you can use JSON Schema or JSG. The latter will test each entry against all of the expected structures so your types simply become constants in the schema:
root { mediums:[contact] }
contact = homephone | email;
homephone {
type: "Home Phone"
value: PHONE
allowContact: BOOL
}
email {
type: "Email"
value: EMAIL
allowContact: BOOL
}
BOOL: "true" | "false" ;
PHONE: [0-9 -.]+;
EMAIL: [^@]* '@' [a-z0-9.]*;
You can play with it. It's not a standard but there is at least one other implementation from Mayo clinic.
Upvotes: 1