var validator = new ObjectValidator
validator.add new RegexField("title", "[A-Z][a-z]+".to_re)
assert not validator.validate("""{ "title": "foo" }""")
assert validator.validate("""{ "title": "Foo" }""")
popcorn :: RegexField :: defaultinit
popcorn $ RegexField :: SELF
Type of this instance, automatically specialized in every classpopcorn $ RegexField :: validate_field
Validatefield
in obj
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
popcorn :: RegexField :: defaultinit
popcorn :: RequiredField :: 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
# Check if a field match a regular expression
#
# ~~~
# var validator = new ObjectValidator
# validator.add new RegexField("title", "[A-Z][a-z]+".to_re)
# assert not validator.validate("""{ "title": "foo" }""")
# assert validator.validate("""{ "title": "Foo" }""")
# ~~~
class RegexField
super RequiredField
autoinit field, re, required
# Regular expression to match
var re: Regex
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 String got `null`")
return false
else
return true
end
end
if not val isa String then
v.validation.add_error(field, "Expected String got `{val.class_name}`")
return false
end
if not val.has(re) then
v.validation.add_error(field, "Does not match `{re.string}`")
return false
end
return true
end
end
lib/popcorn/pop_validation.nit:672,1--709,3