Closed gotjoshua closed 6 years ago
Something like that ?
const MyModel = ObjectModel({
comments: ArrayModel(String),
status: String
}).assert(o => o.status !== 'blocked' || o.comments.length > 0,
"requires the comments property to be non-empty if status property == 'blocked'")
nearly perfect!
I went with:
const MyModel = ObjectModel({
comments: ArrayModel(String),
status: String
}).assert(o => o.status !== 'blocked' || !! o.comments,
"requires the comments property to be non-empty if status property == 'blocked'")
as undefined comments have no length (maybe also because I am using String as my comments type - why do you suggest ArrayModel(String) ? )
Thanks once again!
I did not know what the type of your variable was, I guessed it was an array because you used the plural form, which is a popular convention. If it is an optional string, change the definition to comments: [String]
We have a model that requires the comments property to be non-empty if status property == 'blocked'
Is it possible to make an assertion for one prop that depends on another?