1 /**
2  * Skadi.d Web Framework
3  * Validation Component
4 
5  * Authors: Faianca
6  * Copyright: Copyright (c) 2015 Faianca
7  * License: MIT License, see LICENSE
8  */
9 module skadi.components.validation.validation;
10 
11 import skadi.components.validation.messageinterface;
12 import skadi.components.validation.messagelist;
13 
14 /**
15  * Allows to validate data using custom or built-in validators
16  */
17 class Validation
18 {
19 
20 protected:
21     MessageList _messages;
22 
23     string[string] _labels;
24 
25     enum string[string] _defaultMessages = [
26        "Alnum": "Field %s must contain only letters and numbers",
27        "Alpha": "Field %s must contain only letters",
28        "Range": "Field %s must be within the range of %d to %d",
29        "Confirmation": "Field %s must be the same as :with",
30        "Digit": "Field %s must be numeric",
31        "Email": "Field %s must be an email address",
32        "ExclusionIn": "Field %s must not be a part of list: %s",
33        "FileEmpty": "Field %s must not be empty",
34        "FileIniSize": "File %s exceeds the maximum file size",
35        "FileMaxResolution": "File %s must not exceed %d resolution",
36        "FileMinResolution": "File %s must be at least %d resolution",
37        "FileSize": "File %s exceeds the size of %d",
38        "FileType": "File %s must be of type: :types",
39        "FileValid": "Field %s is not valid",
40        "Identical": "Field %s does not have the expected value",
41        "InclusionIn": "Field %s must be a part of list: %s",
42        "Numericality": "Field %s does not have a valid numeric format",
43        "PresenceOf": "Field %s is required",
44        "Regex": "Field %s does not match the required format",
45        "TooLong": "Field %s must not exceed %d characters long",
46        "TooShort": "Field %s must be at least %d characters long",
47        "Uniqueness": "Field %s must be unique",
48        "Url": "Field %s must be a url"
49    ];
50 
51 public:
52 
53     this()
54     {
55         this._messages = new MessageList();
56     }
57 
58     /**
59 	 * Get default message for validator type
60 	 *
61 	 * @param string type
62 	 */
63 	string getDefaultMessage(string type)
64 	{
65         if (type !in this._defaultMessages) {
66 			return "";
67 		}
68 		return this._defaultMessages[type];
69 	}
70 
71 	/**
72 	 * Appends a message to the messages list
73 	 */
74 	Validation appendMessage(MessageInterface message)
75 	{
76 		this._messages.appendMessage(message);
77 		return this;
78 	}
79 
80     /**
81 	 * Returns the registered validators
82 	 */
83 	MessageList getMessages()
84 	{
85 		return this._messages;
86 	}
87 
88     /**
89 	 * Get label for field
90 	 *
91 	 * @param string field
92 	 * @return string
93 	 */
94 	string getLabel(string field)
95 	{
96         if (field !in this._labels) {
97 			return field;
98 		}
99 		return this._labels[field];
100 	}
101 
102 	/**
103 	 * Gets the a value to validate
104 	 */
105 	string getValue(string field)
106 	{
107         return field;
108 	}
109 
110     bool isValid()
111     {
112         return this._messages.isEmpty();
113     }
114 
115 }