var validator = new ObjectValidator
validator.add new RequiredField("id", required = true)
validator.add new StringField("login", min_size=4)
validator.add new IntField("age", min=0, max=100)
assert not validator.validate("""{}""")
assert not validator.validate("""[]""")
assert validator.validate("""{ "id": "", "login": "Alex", "age": 10 }""")
popcorn :: ObjectValidator :: validate_json
Validate a Serializable inputpopcorn :: ObjectValidator :: validators
Validators to apply on the objectpopcorn :: ObjectValidator :: validators=
Validators to apply on the objectpopcorn $ ObjectValidator :: SELF
Type of this instance, automatically specialized in every classcore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
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 :: 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 a JsonObject
# ~~~
# var validator = new ObjectValidator
# validator.add new RequiredField("id", required = true)
# validator.add new StringField("login", min_size=4)
# validator.add new IntField("age", min=0, max=100)
# assert not validator.validate("""{}""")
# assert not validator.validate("""[]""")
# assert validator.validate("""{ "id": "", "login": "Alex", "age": 10 }""")
# ~~~
class ObjectValidator
super DocumentValidator
# Validators to apply on the object
var validators = new Array[FieldValidator]
redef fun validate(document) do
super
var json = document.parse_json
if json == null then
validation.add_error("document", "Expected JsonObject 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 JsonObject then
validation.add_error("document", "Expected JsonObject got `{json.class_name}`")
return false
end
validation.object = json
for validator in validators do
var res = validator.validate_field(self, json)
if not res then return false
end
return true
end
# Add a validator
fun add(validator: FieldValidator) do validators.add validator
end
lib/popcorn/pop_validation.nit:170,1--212,3