more documentation for validation

This commit is contained in:
Benjamin Neff 2015-07-26 01:19:09 +02:00
parent ce39616265
commit dd6b938f2e
13 changed files with 70 additions and 2 deletions

View file

@ -1,6 +1,8 @@
module DiasporaFederation module DiasporaFederation
module Entities module Entities
# this entity contains the base data of a person # this entity contains the base data of a person
#
# @see Validators::PersonValidator
class Person < Entity class Person < Entity
# @!attribute [r] guid # @!attribute [r] guid
# @see HCard#guid # @see HCard#guid

View file

@ -1,6 +1,8 @@
module DiasporaFederation module DiasporaFederation
module Entities module Entities
# this entity contains all the profile data of a person # this entity contains all the profile data of a person
#
# @see Validators::ProfileValidator
class Profile < Entity class Profile < Entity
# @!attribute [r] diaspora_id # @!attribute [r] diaspora_id
# The diaspora ID of the person # The diaspora ID of the person

View file

@ -38,6 +38,12 @@ module DiasporaFederation
# Initializes the Entity with the given attribute hash and freezes the created # Initializes the Entity with the given attribute hash and freezes the created
# instance it returns. # instance it returns.
# #
# After creation, the entity is validated against a Validator, if one is defined.
# The Validator needs to be in the {DiasporaFederation::Validators} namespace and
# named like "<EntityName>Validator". Only valid entities can be created.
#
# @see DiasporaFederation::Validators
#
# @note Attributes not defined as part of the class definition ({PropertiesDSL#property}, # @note Attributes not defined as part of the class definition ({PropertiesDSL#property},
# {PropertiesDSL#entity}) get discarded silently. # {PropertiesDSL#entity}) get discarded silently.
# #

View file

@ -23,6 +23,9 @@ require "diaspora_federation/validators/rules/tag_count"
module DiasporaFederation module DiasporaFederation
# Validators to perform basic sanity-checks on {DiasporaFederation::Entities federation entities}. # Validators to perform basic sanity-checks on {DiasporaFederation::Entities federation entities}.
#
# The Validators are mapped with the entities by name. The naming schema
# is "<EntityName>Validator".
module Validators module Validators
end end
end end

View file

@ -1,5 +1,6 @@
module DiasporaFederation module DiasporaFederation
module Validators module Validators
# This validates a {Entities::Person}
class PersonValidator < Validation::Validator class PersonValidator < Validation::Validator
include Validation include Validation

View file

@ -1,5 +1,6 @@
module DiasporaFederation module DiasporaFederation
module Validators module Validators
# This validates a {Entities::Profile}
class ProfileValidator < Validation::Validator class ProfileValidator < Validation::Validator
include Validation include Validation

View file

@ -2,11 +2,20 @@ require "date"
module Validation module Validation
module Rule module Rule
# Birthday validation rule
#
# Valid is:
# * nil or an empty +String+
# * a +Date+ object
# * a +String+ with the format "yyyy-mm-dd" and is a valid +Date+, example: 2015-07-25
class Birthday class Birthday
# The error key for this rule
# @return [Symbol] error key
def error_key def error_key
:birthday :birthday
end end
# Determines if value is a valid birthday date
def valid_value?(value) def valid_value?(value)
return true if value.nil? || (value.is_a?(String) && value.empty?) return true if value.nil? || (value.is_a?(String) && value.empty?)
return true if value.is_a? Date return true if value.is_a? Date
@ -20,6 +29,7 @@ module Validation
end end
# This rule has no params # This rule has no params
# @return [Hash] params
def params def params
{} {}
end end

View file

@ -1,10 +1,19 @@
module Validation module Validation
module Rule module Rule
# Boolean validation rule
#
# Valid is:
# * a +String+: "true", "false", "t", "f", "yes", "no", "y", "n", "1", "0"
# * a +Fixnum+: 1 or 0
# * a +Boolean+: true or false
class Boolean class Boolean
# The error key for this rule
# @return [Symbol] error key
def error_key def error_key
:boolean :boolean
end end
# Determines if value is a valid +boolean+
def valid_value?(value) def valid_value?(value)
return false if value.nil? return false if value.nil?
@ -20,6 +29,7 @@ module Validation
end end
# This rule has no params # This rule has no params
# @return [Hash] params
def params def params
{} {}
end end

View file

@ -5,6 +5,7 @@ module Validation
# This rule is based on https://github.com/zombor/Validator/blob/master/lib/validation/rule/email.rb # This rule is based on https://github.com/zombor/Validator/blob/master/lib/validation/rule/email.rb
# which was adapted from https://github.com/emmanuel/aequitas/blob/master/lib/aequitas/rule/format/email_address.rb # which was adapted from https://github.com/emmanuel/aequitas/blob/master/lib/aequitas/rule/format/email_address.rb
class DiasporaId class DiasporaId
# The Regex for a valid diaspora ID
DIASPORA_ID = begin DIASPORA_ID = begin
letter = "a-zA-Z" letter = "a-zA-Z"
digit = "0-9" digit = "0-9"
@ -25,16 +26,18 @@ module Validation
end end
# The error key for this rule # The error key for this rule
# @return [Symbol] error key
def error_key def error_key
:diaspora_id :diaspora_id
end end
# Determines if value is a valid email # Determines if value is a valid diaspora ID
def valid_value?(value) def valid_value?(value)
!DIASPORA_ID.match(value).nil? !DIASPORA_ID.match(value).nil?
end end
# This rule has no params # This rule has no params
# @return [Hash] params
def params def params
{} {}
end end

View file

@ -1,15 +1,25 @@
module Validation module Validation
module Rule module Rule
# GUID validation rule
#
# Valid is a +String+ that is at least 16 chars long and contains only:
# * Letters: a-z
# * Numbers: 0-9
# * Special chars: '-', '_', '@', '.' and ':'
class Guid class Guid
# The error key for this rule
# @return [Symbol] error key
def error_key def error_key
:guid :guid
end end
# Determines if value is a valid +GUID+
def valid_value?(value) def valid_value?(value)
value.is_a?(String) && value.downcase =~ /\A[0-9a-z\-_@.:]{16,}\z/ value.is_a?(String) && value.downcase =~ /\A[0-9a-z\-_@.:]{16,}\z/
end end
# This rule has no params # This rule has no params
# @return [Hash] params
def params def params
{} {}
end end

View file

@ -1,15 +1,20 @@
module Validation module Validation
module Rule module Rule
# Validates that a property is not +nil+
class NotNil class NotNil
# The error key for this rule
# @return [Symbol] error key
def error_key def error_key
:not_nil :not_nil
end end
# Determines if value is not nil
def valid_value?(value) def valid_value?(value)
!value.nil? !value.nil?
end end
# This rule has no params # This rule has no params
# @return [Hash] params
def params def params
{} {}
end end

View file

@ -1,11 +1,19 @@
module Validation module Validation
module Rule module Rule
# Public key validation rule
#
# A valid key must:
# * start with "-----BEGIN PUBLIC KEY-----" and end with "-----END PUBLIC KEY-----"
# or
# * start with "-----BEGIN RSA PUBLIC KEY-----" and end with "-----END RSA PUBLIC KEY-----"
class PublicKey class PublicKey
# The error key for this rule
# @return [Symbol] error key
def error_key def error_key
:public_key :public_key
end end
# allow both "PUBLIC KEY" and "RSA PUBLIC KEY" # Determines if value is a valid public key
def valid_value?(value) def valid_value?(value)
(value.strip.start_with?("-----BEGIN PUBLIC KEY-----") && (value.strip.start_with?("-----BEGIN PUBLIC KEY-----") &&
value.strip.end_with?("-----END PUBLIC KEY-----")) || value.strip.end_with?("-----END PUBLIC KEY-----")) ||
@ -14,6 +22,7 @@ module Validation
end end
# This rule has no params # This rule has no params
# @return [Hash] params
def params def params
{} {}
end end

View file

@ -2,7 +2,10 @@ module Validation
module Rule module Rule
# Rule for validating the number of tags in a string. # Rule for validating the number of tags in a string.
# Only the "#" characters will be counted. # Only the "#" characters will be counted.
# The string can be nil.
class TagCount class TagCount
# This rule must have a +maximum+ param
# @return [Hash] params
attr_reader :params attr_reader :params
# @param [Hash] params # @param [Hash] params
@ -15,10 +18,13 @@ module Validation
@params = params @params = params
end end
# The error key for this rule
# @return [Symbol] error key
def error_key def error_key
:tag_count :tag_count
end end
# Determines if value doesn't have more than +maximum+ tags
def valid_value?(value) def valid_value?(value)
value.nil? || value.count("#") <= params[:maximum] value.nil? || value.count("#") <= params[:maximum]
end end