How to use %ValidateObject with %Dynamic properties?
I am creating a class to validate JSON body of requests. When I use the method %ValidateObject to check errors in the object, if I define some properties with %DynamicObject or %DynamicArray and use the Required parameter, this method does not work, it ignores validation, only works with properties %String, %Integer etc.
Class test.Example Extends%RegisteredObject
{
Property id As%Integer [ Required ];Property name As%String [ Required ];Property fieldOptions As%DynamicArray [ Required ];
Method %OnNew(id As%Integer, name As%String, fieldOptions As%DynamicArray) As%Status [ Private, ServerOnly = 1 ]
{
Set..id = id
Set..name = name
Set..fieldOptions = fieldOptions
}// fieldOptions is ""Set test = ##class(test.Example).%New(1, "name", "")
Set obj = test.%ValidateObject()
Set message = $System.Status.GetOneErrorText(obj, 1)
Write message
// Return is "OK"Comments
Hello @Gabriel Santos
AFAIK, The required property keyword works on literal, Collection, stream, serial, object valued properties except the %DynamicArray and %DynamicObject. Because by default the property methods(Getter) was created and it assign the values for %DynamicArray is "[]" and "{}" for dynamicObject even though if you assign empty string it overwrites it. so, The %ValidateObject() doesn't include these two objects for validation.
You'll need to implement %OnValidateObject callback to do custom checking.
That was very useful, thank you!