var validator = new ArrayValidator
assert not validator.validate("""{}""")
assert validator.validate("""[]""")
assert validator.validate("""[ "id", 10, {} ]""")
validator = new ArrayValidator(allow_empty=false)
assert not validator.validate("""[]""")
assert validator.validate("""[ "id", 10, {} ]""")
validator = new ArrayValidator(length=3)
assert not validator.validate("""[]""")
assert validator.validate("""[ "id", 10, {} ]""")
popcorn :: ArrayValidator :: allow_empty
Allow empty arrays (default: true)popcorn :: ArrayValidator :: allow_empty=
Allow empty arrays (default: true)popcorn :: ArrayValidator :: defaultinit
popcorn :: ArrayValidator :: length=
Check array length (default: no check)popcorn :: ArrayValidator :: validate_json
Validate a Serializable inputpopcorn $ ArrayValidator :: SELF
Type of this instance, automatically specialized in every classpopcorn :: 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 :: ArrayValidator :: 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.
popcorn :: ArrayValidator :: length=
Check array length (default: no check)core :: Object :: output_class_name
Display class name on stdout (debug only).popcorn :: ArrayValidator :: validate_json
Validate a Serializable inputpopcorn :: DocumentValidator :: validation=
Validation result
# Check a JsonArray
# ~~~
# var validator = new ArrayValidator
# assert not validator.validate("""{}""")
# assert validator.validate("""[]""")
# assert validator.validate("""[ "id", 10, {} ]""")
#
# validator = new ArrayValidator(allow_empty=false)
# assert not validator.validate("""[]""")
# assert validator.validate("""[ "id", 10, {} ]""")
#
# validator = new ArrayValidator(length=3)
# assert not validator.validate("""[]""")
# assert validator.validate("""[ "id", 10, {} ]""")
# ~~~
class ArrayValidator
super DocumentValidator
# Allow empty arrays (default: true)
var allow_empty: nullable Bool
# Check array length (default: no check)
var length: nullable Int
redef fun validate(document) do
super
var json = document.parse_json
if json == null then
validation.add_error("document", "Expected JsonArray got `null`")
return false
end
return validate_json(json)
end
# Validate a Serializable input
fun validate_json(json: Serializable): Bool do
if not json isa JsonArray then
validation.add_error("document", "Expected JsonArray got `{json.class_name}`")
return false
end
validation.array = json
var allow_empty = self.allow_empty
if json.is_empty and (allow_empty != null and not allow_empty) then
validation.add_error("document", "Cannot be empty")
return false
end
var length = self.length
if length != null and json.length != length then
validation.add_error("document", "Array length must be exactly `{length}`")
return false
end
return true
end
end
lib/popcorn/pop_validation.nit:214,1--268,3