var validator = new ObjectValidator
validator.add new RequiredField("id", required = true)
validator.add new ArrayField("orders", allow_empty=false)
assert not validator.validate("""{ "id": "", "orders": [] }""")
assert validator.validate("""{ "id": "", "orders": [ 1 ] }""")
popcorn :: ArrayField :: defaultinit
popcorn $ ArrayField :: SELF
Type of this instance, automatically specialized in every classpopcorn $ ArrayField :: validate_field
Validatefield
in obj
popcorn $ ArrayField :: validation=
Validation resultpopcorn :: ArrayValidator :: allow_empty
Allow empty arrays (default: true)popcorn :: ArrayValidator :: allow_empty=
Allow empty arrays (default: true)core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
popcorn :: RequiredField :: defaultinit
popcorn :: FieldValidator :: defaultinit
popcorn :: ArrayField :: defaultinit
core :: Object :: defaultinit
popcorn :: ArrayValidator :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
popcorn :: ArrayValidator :: length=
Check array length (default: no check)core :: Object :: output_class_name
Display class name on stdout (debug only).popcorn :: RequiredField :: required=
Is this field required?popcorn :: FieldValidator :: validate_field
Validatefield
in obj
popcorn :: ArrayValidator :: validate_json
Validate a Serializable inputpopcorn :: DocumentValidator :: validation=
Validation result
# Check that a field is a JsonArray
#
# ~~~
# var validator = new ObjectValidator
# validator.add new RequiredField("id", required = true)
# validator.add new ArrayField("orders", allow_empty=false)
# assert not validator.validate("""{ "id": "", "orders": [] }""")
# assert validator.validate("""{ "id": "", "orders": [ 1 ] }""")
# ~~~
class ArrayField
super RequiredField
super ArrayValidator
autoinit field=, required=, allow_empty=, length=
redef var validation = new ValidationResult
redef fun validate_field(v, obj) do
if not super then return false
var val = obj.get_or_null(field)
if val == null then
if required == null or required == true then
v.validation.add_error(field, "Expected Array got `null`")
return false
else
return true
end
end
var res = validate_json(val)
for field, messages in validation.errors do
for message in messages do v.validation.add_error("{self.field}.{field}", message)
end
return res
end
end
lib/popcorn/pop_validation.nit:603,1--637,3