Can be convertted to a JsonObject so it can be reterned in a Json HttpResponse.
Errors messages are grouped into scopes. A scope is a string that specify wich field or document the error message is related to.
popcorn :: ValidationResult :: array=
Array parsed during validationpopcorn :: ValidationResult :: object
Object parsed during validationpopcorn :: ValidationResult :: object=
Object parsed during validationpopcorn :: ValidationResult :: to_pretty_string
Returns the validation result as a pretty formated stringpopcorn $ ValidationResult :: SELF
Type of this instance, automatically specialized in every classpopcorn $ ValidationResult :: core_serialize_to
Actual serialization ofself
to serializer
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
popcorn :: ValidationResult :: array=
Array parsed during validationcore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
core :: Object :: defaultinit
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
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.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraypopcorn :: ValidationResult :: object
Object parsed during validationpopcorn :: ValidationResult :: object=
Object parsed during validationcore :: Object :: output_class_name
Display class name on stdout (debug only).serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
popcorn :: ValidationResult :: to_pretty_string
Returns the validation result as a pretty formated stringSerializer::serialize
# Validation Result representation
#
# Can be convertted to a JsonObject so it can be reterned in a Json HttpResponse.
#
# Errors messages are grouped into *scopes*. A scope is a string that specify wich
# field or document the error message is related to.
class ValidationResult
super Serializable
# Object parsed during validation
#
# Can be used as a quick way to access the parsed JsonObject instead of
# reparsing it during the answer.
#
# See `ObjectValidator`.
var object: nullable JsonObject = null is writable
# Array parsed during validation
#
# Can be used as a quick way to access the parsed JsonArray instead of
# reparsing it during the answer.
#
# See `ArrayValidator`.
var array: nullable JsonArray = null is writable
# Errors found during validation
#
# Errors are grouped by scope.
var errors = new HashMap[String, Array[String]]
# Generate a new error `message` into `scope`
fun add_error(scope, message: String) do
if not errors.has_key(scope) then
errors[scope] = new Array[String]
end
errors[scope].add message
end
# Get the errors for `scope`
fun error(scope: String): Array[String] do
if not errors.has_key(scope) then
return new Array[String]
end
return errors[scope]
end
# Does `self` contains `errors`?
fun has_error: Bool do return errors.not_empty
redef fun core_serialize_to(v) do
var errors = new JsonObject
for k, e in self.errors do
errors[k] = new JsonArray.from(e)
end
v.serialize_attribute("has_error", has_error)
v.serialize_attribute("errors", errors)
end
# Returns the validation result as a pretty formated string
fun to_pretty_string: String do
var b = new Buffer
if not has_error then
b.append "Everything is correct\n"
else
b.append "There is errors\n\n"
for k, v in errors do
b.append "{k}:\n"
for vv in v do
b.append "\t{vv}\n"
end
b.append "\n"
end
end
return b.write_to_string
end
end
lib/popcorn/pop_validation.nit:93,1--168,3