UNPKG

11.3 kBTypeScriptView Raw
1// Copyright IBM Corp. 2018. All Rights Reserved.
2// Node module: loopback-datasource-juggler
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6import {Callback, Options, PromiseOrVoid} from './common';
7
8/**
9 * This class provides methods that add validation cababilities to models.
10 * Each of the validations runs when the `obj.isValid()` method is called.
11 *
12 * All of the methods have an options object parameter that has a
13 * `message` property. When there is only a single error message, this
14 * property is just a string;
15 * for example: `Post.validatesPresenceOf('title', { message: 'can not be blank' });`
16 *
17 * In more complicated cases it can be a set of messages, for each possible
18 * error condition; for example:
19 * `User.validatesLengthOf('password', { min: 6, max: 20, message: {min: 'too short', max: 'too long'}});`
20 *
21 */
22export interface Validatable {
23 /**
24 * Validate presence of one or more specified properties.
25 *
26 * Requires a model to include a property to be considered valid; fails when validated field is blank.
27 *
28 * For example, validate presence of title
29 * ```
30 * Post.validatesPresenceOf('title');
31 * ```
32 * Validate that model has first, last, and age properties:
33 * ```
34 * User.validatesPresenceOf('first', 'last', 'age');
35 * ```
36 * Example with custom message
37 * ```
38 * Post.validatesPresenceOf('title', {message: 'Cannot be blank'});
39 * ```
40 *
41 * @param {String} propertyName One or more property names.
42 * @options {Object} options Configuration parameters; see below.
43 * @property {String} message Error message to use instead of default.
44 * @property {String} if Validate only if `if` exists.
45 * @property {String} unless Validate only if `unless` exists.
46 */
47 validatesPresenceOf(...propertyNames: string[]): void;
48 validatesPresenceOf(propertyName: string, options?: Options): void;
49
50 /**
51 * Validate absence of one or more specified properties.
52 *
53 * A model should not include a property to be considered valid; fails when validated field is not blank.
54 *
55 * For example, validate absence of reserved
56 * ```
57 * Post.validatesAbsenceOf('reserved', { unless: 'special' });
58 * ```
59 *
60 * @param {String} propertyName One or more property names.
61 * @options {Object} options Configuration parameters; see below.
62 * @property {String} message Error message to use instead of default.
63 * @property {String} if Validate only if `if` exists.
64 * @property {String} unless Validate only if `unless` exists.
65 */
66 validatesAbsenceOf(...propertyNames: string[]): void;
67 validatesAbsenceOf(propertyName: string, options?: Options): void;
68
69 /**
70 * Validate length.
71 *
72 * Require a property length to be within a specified range.
73 *
74 * There are three kinds of validations: min, max, is.
75 *
76 * Default error messages:
77 *
78 * - min: too short
79 * - max: too long
80 * - is: length is wrong
81 *
82 * Example: length validations
83 * ```
84 * User.validatesLengthOf('password', {min: 7});
85 * User.validatesLengthOf('email', {max: 100});
86 * User.validatesLengthOf('state', {is: 2});
87 * User.validatesLengthOf('nick', {min: 3, max: 15});
88 * ```
89 * Example: length validations with custom error messages
90 * ```
91 * User.validatesLengthOf('password', {min: 7, message: {min: 'too weak'}});
92 * User.validatesLengthOf('state', {is: 2, message: {is: 'is not valid state name'}});
93 * ```
94 *
95 * @param {String} propertyName Property name to validate.
96 * @options {Object} options Configuration parameters; see below.
97 * @property {Number} is Value that property must equal to validate.
98 * @property {Number} min Value that property must be less than to be valid.
99 * @property {Number} max Value that property must be less than to be valid.
100 * @property {Object} message Optional object with string properties for custom error message for each validation: is, min, or max.
101 */
102 validatesLengthOf(propertyName: string, options?: Options): void;
103
104 /**
105 * Validate numericality.
106 *
107 * Requires a value for property to be either an integer or number.
108 *
109 * Example
110 * ```
111 * User.validatesNumericalityOf('age', { message: { number: 'is not a number' }});
112 * User.validatesNumericalityOf('age', {int: true, message: { int: 'is not an integer' }});
113 * ```
114 *
115 * @param {String} propertyName Property name to validate.
116 * @options {Object} options Configuration parameters; see below.
117 * @property {Boolean} int If true, then property must be an integer to be valid.
118 * @property {Boolean} allowBlank Allow property to be blank.
119 * @property {Boolean} allowNull Allow property to be null.
120 * @property {Object} message Optional object with string properties for 'int' for integer validation. Default error messages:
121 * - number: is not a number
122 * - int: is not an integer
123 */
124 validatesNumericalityOf(propertyName: string, options?: Options): void;
125
126 /**
127 * Validate inclusion in set.
128 *
129 * Require a value for property to be in the specified array.
130 *
131 * Example:
132 * ```
133 * User.validatesInclusionOf('gender', {in: ['male', 'female']});
134 * User.validatesInclusionOf('role', {
135 * in: ['admin', 'moderator', 'user'], message: 'is not allowed'
136 * });
137 * ```
138 *
139 * @param {String} propertyName Property name to validate.
140 * @options {Object} options Configuration parameters; see below.
141 * @property {Array} in Property must match one of the values in the array to be valid.
142 * @property {String} message Optional error message if property is not valid.
143 * Default error message: "is not included in the list".
144 * @property {Boolean} allowNull Whether null values are allowed.
145 */
146 validatesInclusionOf(propertyName: string, options?: Options): void;
147
148 /**
149 * Validate exclusion in a set.
150 *
151 * Require a property value not be in the specified array.
152 *
153 * Example: `Company.validatesExclusionOf('domain', {in: ['www', 'admin']});`
154 *
155 * @param {String} propertyName Property name to validate.
156 * @options {Object} options Configuration parameters; see below.
157 * @property {Array} in Property must not match any of the values in the array to be valid.
158 * @property {String} message Optional error message if property is not valid. Default error message: "is reserved".
159 * @property {Boolean} allowNull Whether null values are allowed.
160 */
161 validatesExclusionOf(propertyName: string, options?: Options): void;
162
163 /**
164 * Validate format.
165 *
166 * Require a model to include a property that matches the given format.
167 *
168 * Example: `User.validatesFormatOf('name', {with: /\w+/});`
169 *
170 * @param {String} propertyName Property name to validate.
171 * @options {Object} options Configuration parameters; see below.
172 * @property {RegExp} with Regular expression to validate format.
173 * @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
174 * @property {Boolean} allowNull Whether null values are allowed.
175 */
176 validatesFormatOf(propertyName: string, options?: Options): void;
177
178 /**
179 * Validate using custom validation function.
180 *
181 * Example:
182 *```javascript
183 * User.validate('name', customValidator, {message: 'Bad name'});
184 * function customValidator(err) {
185 * if (this.name === 'bad') err();
186 * });
187 * var user = new User({name: 'Peter'});
188 * user.isValid(); // true
189 * user.name = 'bad';
190 * user.isValid(); // false
191 * ```
192 *
193 * @param {String} propertyName Property name to validate.
194 * @param {Function} validatorFn Custom validation function.
195 * @options {Object} options Configuration parameters; see below.
196 * @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
197 * @property {Boolean} allowNull Whether null values are allowed.
198 */
199 validate(
200 propertyName: string,
201 validatorFn: Function,
202 options?: Options,
203 ): void;
204
205 /**
206 * Validate using custom asynchronous validation function.
207 *
208 * Example:
209 *```js
210 * User.validateAsync('name', customValidator, {message: 'Bad name'});
211 * function customValidator(err, done) {
212 * process.nextTick(function () {
213 * if (this.name === 'bad') err();
214 * done();
215 * });
216 * });
217 * var user = new User({name: 'Peter'});
218 * user.isValid(); // false (because async validation setup)
219 * user.isValid(function (isValid) {
220 * isValid; // true
221 * })
222 * user.name = 'bad';
223 * user.isValid(); // false
224 * user.isValid(function (isValid) {
225 * isValid; // false
226 * })
227 * ```
228 *
229 * @param {String} propertyName Property name to validate.
230 * @param {Function} validatorFn Custom validation function.
231 * @options {Object} options Configuration parameters; see below.
232 * @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
233 * @property {Boolean} allowNull Whether null values are allowed.
234 */
235 validateAsync(
236 propertyName: string,
237 validatorFn: Function,
238 options?: Options,
239 ): void;
240
241 /**
242 * Validate uniqueness of the value for a property in the collection of models.
243 *
244 * Not available for all connectors. Currently supported with these connectors:
245 * - In Memory
246 * - Oracle
247 * - MongoDB
248 *
249 * ```
250 * // The login must be unique across all User instances.
251 * User.validatesUniquenessOf('login');
252 *
253 * // Assuming SiteUser.belongsTo(Site)
254 * // The login must be unique within each Site.
255 * SiteUser.validateUniquenessOf('login', { scopedTo: ['siteId'] });
256 * ```
257 *
258 * @param {String} propertyName Property name to validate.
259 * @options {Object} options Configuration parameters; see below.
260 * @property {RegExp} with Regular expression to validate format.
261 * @property {Array.<String>} scopedTo List of properties defining the scope.
262 * @property {String} message Optional error message if property is not valid. Default error message: "is not unique".
263 * @property {Boolean} allowNull Whether null values are allowed.
264 * @property {String} ignoreCase Make the validation case insensitive.
265 * @property {String} if Validate only if `if` exists.
266 * @property {String} unless Validate only if `unless` exists.
267 */
268 validatesUniquenessOf(
269 propertyName: string,
270 validatorFn: Function,
271 options?: Options,
272 ): void;
273
274 /**
275 * Validate if a value for a property is a Date.
276 *
277 * Example
278 * ```
279 * User.validatesDateOf('today', {message: 'today is not a date!'});
280 * ```
281 *
282 * @param {String} propertyName Property name to validate.
283 * @options {Object} options Configuration parameters; see below.
284 * @property {String} message Error message to use instead of default.
285 */
286 validatesDateOf(
287 propertyName: string,
288 validatorFn: Function,
289 options?: Options,
290 ): void;
291}
292
293/**
294 * ValidationError
295 */
296export declare class ValidationError extends Error {
297 statusCode?: number;
298 details: {
299 context: any;
300 codes: string[];
301 messages: string[];
302 };
303}