var validator = new ObjectValidator
validator.add new RequiredField("id", required = true)
var user_val = new ObjectField("user")
user_val.add new RequiredField("id", required = true)
user_val.add new StringField("login", min_size=4)
validator.add user_val
assert not validator.validate("""{ "id": "", "user": { "login": "Alex" } }""")
assert validator.validate("""{ "id": "", "user": { "id": "foo", "login": "Alex" } }""")
popcorn :: ObjectField :: defaultinit
popcorn $ ObjectField :: SELF
Type of this instance, automatically specialized in every classpopcorn $ ObjectField :: validate_field
Validatefield
in obj
popcorn $ ObjectField :: validation=
Validation resultcore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
popcorn :: RequiredField :: defaultinit
popcorn :: ObjectField :: defaultinit
popcorn :: FieldValidator :: defaultinit
core :: Object :: 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.
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 :: ObjectValidator :: validate_json
Validate a Serializable inputpopcorn :: DocumentValidator :: validation=
Validation resultpopcorn :: ObjectValidator :: validators
Validators to apply on the objectpopcorn :: ObjectValidator :: validators=
Validators to apply on the object
# Check that a field is a JsonObject
#
# ~~~
# var validator = new ObjectValidator
# validator.add new RequiredField("id", required = true)
# var user_val = new ObjectField("user")
# user_val.add new RequiredField("id", required = true)
# user_val.add new StringField("login", min_size=4)
# validator.add user_val
# assert not validator.validate("""{ "id": "", "user": { "login": "Alex" } }""")
# assert validator.validate("""{ "id": "", "user": { "id": "foo", "login": "Alex" } }""")
# ~~~
class ObjectField
super RequiredField
super ObjectValidator
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 Object 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:566,1--601,3