var validator = new ObjectValidator
validator.add new BoolField("field", required=false)
assert validator.validate("""{}""")
assert validator.validate("""{ "field": true }""")
assert validator.validate("""{ "field": false }""")
assert not validator.validate("""{ "field": "foo" }""")
validator = new ObjectValidator
validator.add new BoolField("field")
assert not validator.validate("""{}""")
assert validator.validate("""{ "field": true }""")
assert validator.validate("""{ "field": false }""")
assert not validator.validate("""{ "field": "foo" }""")
No type conversion is applied on the input value:
assert not validator.validate("""{ "field": "true" }""")
assert not validator.validate("""{ "field": 1 }""")
assert not validator.validate("""{ "field": [true] }""")
popcorn :: BoolField :: defaultinit
popcorn $ BoolField :: validate_field
Validatefield
in obj
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
popcorn :: FieldValidator :: defaultinit
popcorn :: RequiredField :: defaultinit
popcorn :: BoolField :: 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 is a Bool
#
# ~~~
# var validator = new ObjectValidator
# validator.add new BoolField("field", required=false)
# assert validator.validate("""{}""")
# assert validator.validate("""{ "field": true }""")
# assert validator.validate("""{ "field": false }""")
# assert not validator.validate("""{ "field": "foo" }""")
#
# validator = new ObjectValidator
# validator.add new BoolField("field")
# assert not validator.validate("""{}""")
# assert validator.validate("""{ "field": true }""")
# assert validator.validate("""{ "field": false }""")
# assert not validator.validate("""{ "field": "foo" }""")
# ~~~
#
# No type conversion is applied on the input value:
# ~~~
# assert not validator.validate("""{ "field": "true" }""")
# assert not validator.validate("""{ "field": 1 }""")
# assert not validator.validate("""{ "field": [true] }""")
# ~~~
class BoolField
super RequiredField
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 Bool got `null`")
return false
else
return true
end
end
if not val isa Bool then
v.validation.add_error(field, "Expected Bool got `{val.class_name}`")
return false
end
return true
end
end
lib/popcorn/pop_validation.nit:520,1--564,3