Validate field in obj

Property definitions

popcorn $ FieldValidator :: validate_field
	# Validate `field` in `obj`
	fun validate_field(v: ObjectValidator, obj: JsonObject): Bool is abstract
lib/popcorn/pop_validation.nit:276,2--277,74

popcorn $ RequiredField :: validate_field
	redef fun validate_field(v, obj) do
		var required = self.required
		if (required != null and required or required == null) and not obj.has_key(field) then
			v.validation.add_error(field, "Required field")
			return false
		end
		return true
	end
lib/popcorn/pop_validation.nit:304,2--311,4

popcorn $ FieldsMatch :: validate_field
	redef fun validate_field(v, obj) do
		var val1 = obj.get_or_null(field)
		var val2 = obj.get_or_null(other)
		if val1 != val2 then
			v.validation.add_error(field, "Values mismatch: `{val1 or else "null"}` against `{val2 or else "null"}`")
			return false
		end
		return true
	end
lib/popcorn/pop_validation.nit:661,2--669,4

popcorn $ StringField :: validate_field
	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
		var min_size = self.min_size
		if min_size != null and val.length < min_size then
			v.validation.add_error(field, "Must be at least `{min_size} characters long`")
			return false
		end
		var max_size = self.max_size
		if max_size != null and val.length > max_size then
			v.validation.add_error(field, "Must be at max `{max_size} characters long`")
			return false
		end
		return true
	end
lib/popcorn/pop_validation.nit:355,2--381,4

popcorn $ IntField :: validate_field
	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 Int got `null`")
				return false
			else
				return true
			end
		end
		if not val isa Int then
			v.validation.add_error(field, "Expected Int got `{val.class_name}`")
			return false
		end
		var min = self.min
		if min != null and val < min then
			v.validation.add_error(field, "Must be greater or equal to `{min}`")
			return false
		end
		var max = self.max
		if max != null and val > max then
			v.validation.add_error(field, "Must be smaller or equal to `{max}`")
			return false
		end
		return true
	end
lib/popcorn/pop_validation.nit:423,2--449,4

popcorn $ FloatField :: validate_field
	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 Float got `null`")
				return false
			else
				return true
			end
		end
		if not val isa Float then
			v.validation.add_error(field, "Expected Float got `{val.class_name}`")
			return false
		end
		var min = self.min
		if min != null and val < min then
			v.validation.add_error(field, "Must be smaller or equal to `{min}`")
			return false
		end
		var max = self.max
		if max != null and val > max then
			v.validation.add_error(field, "Must be greater or equal to `{max}`")
			return false
		end
		return true
	end
lib/popcorn/pop_validation.nit:491,2--517,4

popcorn $ BoolField :: validate_field
	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
lib/popcorn/pop_validation.nit:547,2--563,4

popcorn $ RegexField :: validate_field
	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
lib/popcorn/pop_validation.nit:688,2--708,4

popcorn $ UniqueField :: validate_field
	redef fun validate_field(v, obj) do
		if not super then return false
		var val = obj.get_or_null(field)
		if not val isa String then return false
		if not check_unicity(v, field, val) then return false
		return true
	end
lib/popcorn/pop_validation.nit:803,2--809,4

popcorn $ ObjectField :: validate_field
	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 Object got `null`")
				return false
			else
				return true
			end
		end
		var res = validate_json(val)
		for field, messages in validation.errors do
			for message in messages do v.validation.add_error("{self.field}.{field}", message)
		end
		return res
	end
lib/popcorn/pop_validation.nit:584,2--600,4

popcorn $ ArrayField :: validate_field
	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 Array got `null`")
				return false
			else
				return true
			end
		end
		var res = validate_json(val)
		for field, messages in validation.errors do
			for message in messages do v.validation.add_error("{self.field}.{field}", message)
		end
		return res
	end
lib/popcorn/pop_validation.nit:620,2--636,4