7 } from '@angular/forms';
9 import _ from 'lodash';
10 import { Observable, of as observableOf, timer as observableTimer } from 'rxjs';
11 import { map, switchMapTo, take } from 'rxjs/operators';
13 import { DimlessBinaryPipe } from '../pipes/dimless-binary.pipe';
14 import { FormatterService } from '../services/formatter.service';
16 export function isEmptyInputValue(value: any): boolean {
17 return value == null || value.length === 0;
20 export type existsServiceFn = (value: any) => Observable<boolean>;
22 export class CdValidators {
24 * Validator that performs email validation. In contrast to the Angular
25 * email validator an empty email will not be handled as invalid.
27 static email(control: AbstractControl): ValidationErrors | null {
28 // Exit immediately if value is empty.
29 if (isEmptyInputValue(control.value)) {
32 return Validators.email(control);
36 * Validator function in order to validate IP addresses.
37 * @param {number} version determines the protocol version. It needs to be set to 4 for IPv4 and
38 * to 6 for IPv6 validation. For any other number (it's also the default case) it will return a
39 * function to validate the input string against IPv4 OR IPv6.
40 * @returns {ValidatorFn} A validator function that returns an error map containing `pattern`
41 * if the validation check fails, otherwise `null`.
43 static ip(version: number = 0): ValidatorFn {
46 /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i;
47 const ipv6Rgx = /^(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$/i;
50 return Validators.pattern(ipv4Rgx);
51 } else if (version === 6) {
52 return Validators.pattern(ipv6Rgx);
54 return Validators.pattern(new RegExp(ipv4Rgx.source + '|' + ipv6Rgx.source));
59 * Validator function in order to validate numbers.
60 * @returns {ValidatorFn} A validator function that returns an error map containing `pattern`
61 * if the validation check fails, otherwise `null`.
63 static number(allowsNegative: boolean = true): ValidatorFn {
65 return Validators.pattern(/^-?[0-9]+$/i);
67 return Validators.pattern(/^[0-9]+$/i);
72 * Validator function in order to validate decimal numbers.
73 * @returns {ValidatorFn} A validator function that returns an error map containing `pattern`
74 * if the validation check fails, otherwise `null`.
76 static decimalNumber(allowsNegative: boolean = true): ValidatorFn {
78 return Validators.pattern(/^-?[0-9]+(.[0-9]+)?$/i);
80 return Validators.pattern(/^[0-9]+(.[0-9]+)?$/i);
85 * Validator that performs SSL certificate validation.
86 * @returns {ValidatorFn} A validator function that returns an error map containing `pattern`
87 * if the validation check fails, otherwise `null`.
89 static sslCert(): ValidatorFn {
90 return Validators.pattern(
91 /^-----BEGIN CERTIFICATE-----(\n|\r|\f)((.+)?((\n|\r|\f).+)*)(\n|\r|\f)-----END CERTIFICATE-----[\n\r\f]*$/
96 * Validator that performs SSL private key validation.
97 * @returns {ValidatorFn} A validator function that returns an error map containing `pattern`
98 * if the validation check fails, otherwise `null`.
100 static sslPrivKey(): ValidatorFn {
101 return Validators.pattern(
102 /^-----BEGIN RSA PRIVATE KEY-----(\n|\r|\f)((.+)?((\n|\r|\f).+)*)(\n|\r|\f)-----END RSA PRIVATE KEY-----[\n\r\f]*$/
107 * Validator that requires controls to fulfill the specified condition if
108 * the specified prerequisites matches. If the prerequisites are fulfilled,
109 * then the given function is executed and if it succeeds, the 'required'
110 * validation error will be returned, otherwise null.
111 * @param {Object} prerequisites An object containing the prerequisites.
112 * To do additional checks rather than checking for equality you can
113 * use the extended prerequisite syntax:
114 * 'field_name': { 'op': '<OPERATOR>', arg1: '<OPERATOR_ARGUMENT>' }
115 * The following operators are supported:
124 * 'generate_key': true,
125 * 'username': 'Max Mustermann'
128 * ### Example - Extended prerequisites
131 * 'generate_key': { 'op': 'equal', 'arg1': true },
132 * 'username': { 'op': 'minLength', 'arg1': 5 }
135 * Only if all prerequisites are fulfilled, then the validation of the
136 * control will be triggered.
137 * @param {Function | undefined} condition The function to be executed when all
138 * prerequisites are fulfilled. If not set, then the {@link isEmptyInputValue}
139 * function will be used by default. The control's value is used as function
140 * argument. The function must return true to set the validation error.
141 * @return {ValidatorFn} Returns the validator function.
143 static requiredIf(prerequisites: object, condition?: Function | undefined): ValidatorFn {
144 let isWatched = false;
146 return (control: AbstractControl): ValidationErrors | null => {
147 if (!isWatched && control.parent) {
148 Object.keys(prerequisites).forEach((key) => {
149 control.parent.get(key).valueChanges.subscribe(() => {
150 control.updateValueAndValidity({ emitEvent: false });
157 // Check if all prerequisites met.
159 !Object.keys(prerequisites).every((key) => {
160 if (!control.parent) {
163 const value = control.parent.get(key).value;
164 const prerequisite = prerequisites[key];
165 if (_.isObjectLike(prerequisite)) {
167 switch (prerequisite['op']) {
169 result = _.isEmpty(value);
172 result = !_.isEmpty(value);
175 result = value === prerequisite['arg1'];
178 result = value !== prerequisite['arg1'];
181 if (_.isString(value)) {
182 result = value.length >= prerequisite['arg1'];
188 return value === prerequisite;
193 const success = _.isFunction(condition)
194 ? condition.call(condition, control.value)
195 : isEmptyInputValue(control.value);
196 return success ? { required: true } : null;
201 * Compose multiple validators into a single function that returns the union of
202 * the individual error maps for the provided control when the given prerequisites
205 * @param {Object} prerequisites An object containing the prerequisites as
210 * 'generate_key': true,
211 * 'username': 'Max Mustermann'
214 * @param {ValidatorFn[]} validators List of validators that should be taken
215 * into action when the prerequisites are met.
216 * @return {ValidatorFn} Returns the validator function.
218 static composeIf(prerequisites: object, validators: ValidatorFn[]): ValidatorFn {
219 let isWatched = false;
220 return (control: AbstractControl): ValidationErrors | null => {
221 if (!isWatched && control.parent) {
222 Object.keys(prerequisites).forEach((key) => {
223 control.parent.get(key).valueChanges.subscribe(() => {
224 control.updateValueAndValidity({ emitEvent: false });
229 // Check if all prerequisites are met.
231 !Object.keys(prerequisites).every((key) => {
232 return control.parent && control.parent.get(key).value === prerequisites[key];
237 return Validators.compose(validators)(control);
242 * Custom validation by passing a name for the error and a function as error condition.
244 * @param {string} error
245 * @param {Function} condition - a truthy return value will trigger the error
246 * @returns {ValidatorFn}
248 static custom(error: string, condition: Function): ValidatorFn {
249 return (control: AbstractControl): { [key: string]: any } => {
250 const value = condition.call(this, control.value);
252 return { [error]: value };
259 * Validate form control if condition is true with validators.
261 * @param {AbstractControl} formControl
262 * @param {Function} condition
263 * @param {ValidatorFn[]} conditionalValidators List of validators that should only be tested
264 * when the condition is met
265 * @param {ValidatorFn[]} permanentValidators List of validators that should always be tested
266 * @param {AbstractControl[]} watchControls List of controls that the condition depend on.
267 * Every time one of this controls value is updated, the validation will be triggered
270 formControl: AbstractControl,
272 conditionalValidators: ValidatorFn[],
273 permanentValidators: ValidatorFn[] = [],
274 watchControls: AbstractControl[] = []
276 conditionalValidators = conditionalValidators.concat(permanentValidators);
278 formControl.setValidators((control: AbstractControl): {
281 const value = condition.call(this);
283 return Validators.compose(conditionalValidators)(control);
285 if (permanentValidators.length > 0) {
286 return Validators.compose(permanentValidators)(control);
291 watchControls.forEach((control: AbstractControl) => {
292 control.valueChanges.subscribe(() => {
293 formControl.updateValueAndValidity({ emitEvent: false });
299 * Validator that requires that both specified controls have the same value.
300 * Error will be added to the `path2` control.
301 * @param {string} path1 A dot-delimited string that define the path to the control.
302 * @param {string} path2 A dot-delimited string that define the path to the control.
303 * @return {ValidatorFn} Returns a validator function that always returns `null`.
304 * If the validation fails an error map with the `match` property will be set
305 * on the `path2` control.
307 static match(path1: string, path2: string): ValidatorFn {
308 return (control: AbstractControl): { [key: string]: any } => {
309 const ctrl1 = control.get(path1);
310 const ctrl2 = control.get(path2);
311 if (!ctrl1 || !ctrl2) {
314 if (ctrl1.value !== ctrl2.value) {
315 ctrl2.setErrors({ match: true });
317 const hasError = ctrl2.hasError('match');
319 // Remove the 'match' error. If no more errors exists, then set
320 // the error value to 'null', otherwise the field is still marked
322 const errors = ctrl2.errors;
323 _.unset(errors, 'match');
324 ctrl2.setErrors(_.isEmpty(_.keys(errors)) ? null : errors);
332 * Asynchronous validator that requires the control's value to be unique.
333 * The validation is only executed after the specified delay. Every
334 * keystroke during this delay will restart the timer.
335 * @param serviceFn {existsServiceFn} The service function that is
336 * called to check whether the given value exists. It must return
337 * boolean 'true' if the given value exists, otherwise 'false'.
338 * @param serviceFnThis {any} The object to be used as the 'this' object
339 * when calling the serviceFn function. Defaults to null.
340 * @param {number|Date} dueTime The delay time to wait before the
341 * serviceFn call is executed. This is useful to prevent calls on
342 * every keystroke. Defaults to 500.
343 * @return {AsyncValidatorFn} Returns an asynchronous validator function
344 * that returns an error map with the `notUnique` property if the
345 * validation check succeeds, otherwise `null`.
348 serviceFn: existsServiceFn,
349 serviceFnThis: any = null,
350 usernameFn?: Function,
353 ): AsyncValidatorFn {
355 return (control: AbstractControl): Observable<ValidationErrors | null> => {
356 // Exit immediately if user has not interacted with the control yet
357 // or the control value is empty.
358 if (control.pristine || isEmptyInputValue(control.value)) {
359 return observableOf(null);
361 uname = control.value;
362 if (_.isFunction(usernameFn) && usernameFn() !== null && usernameFn() !== '') {
364 uname = `${control.value}$${usernameFn()}`;
366 uname = `${usernameFn()}$${control.value}`;
370 // Forgot previous requests if a new one arrives within the specified
372 return observableTimer(dueTime).pipe(
373 switchMapTo(serviceFn.call(serviceFnThis, uname)),
374 map((resp: boolean) => {
378 return { notUnique: true };
387 * Validator function for UUIDs.
388 * @param required - Defines if it is mandatory to fill in the UUID
389 * @return Validator function that returns an error object containing `invalidUuid` if the
390 * validation failed, `null` otherwise.
392 static uuid(required = false): ValidatorFn {
393 const uuidRe = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
394 return (control: AbstractControl): { [key: string]: any } | null => {
395 if (control.pristine && control.untouched) {
397 } else if (!required && !control.value) {
399 } else if (uuidRe.test(control.value)) {
402 return { invalidUuid: 'This is not a valid UUID' };
407 * A simple minimum validator vor cd-binary inputs.
409 * To use the validation message pass I18n into the function as it cannot
410 * be called in a static one.
412 static binaryMin(bytes: number): ValidatorFn {
413 return (control: AbstractControl): { [key: string]: () => string } | null => {
414 const formatterService = new FormatterService();
415 const currentBytes = new FormatterService().toBytes(control.value);
416 if (bytes <= currentBytes) {
419 const value = new DimlessBinaryPipe(formatterService).transform(bytes);
421 binaryMin: () => $localize`Size has to be at least ${value} or more`
427 * A simple maximum validator vor cd-binary inputs.
429 * To use the validation message pass I18n into the function as it cannot
430 * be called in a static one.
432 static binaryMax(bytes: number): ValidatorFn {
433 return (control: AbstractControl): { [key: string]: () => string } | null => {
434 const formatterService = new FormatterService();
435 const currentBytes = formatterService.toBytes(control.value);
436 if (bytes >= currentBytes) {
439 const value = new DimlessBinaryPipe(formatterService).transform(bytes);
441 binaryMax: () => $localize`Size has to be at most ${value} or less`
447 * Asynchronous validator that checks if the password meets the password
449 * @param userServiceThis The object to be used as the 'this' object
450 * when calling the 'validatePassword' method of the 'UserService'.
451 * @param usernameFn Function to get the username that should be
452 * taken into account.
453 * @param callback Callback function that is called after the validation
455 * @return {AsyncValidatorFn} Returns an asynchronous validator function
456 * that returns an error map with the `passwordPolicy` property if the
457 * validation check fails, otherwise `null`.
459 static passwordPolicy(
460 userServiceThis: any,
461 usernameFn?: Function,
462 callback?: (valid: boolean, credits?: number, valuation?: string) => void
463 ): AsyncValidatorFn {
464 return (control: AbstractControl): Observable<ValidationErrors | null> => {
465 if (control.pristine || control.value === '') {
466 if (_.isFunction(callback)) {
469 return observableOf(null);
472 if (_.isFunction(usernameFn)) {
473 username = usernameFn();
475 return observableTimer(500).pipe(
476 switchMapTo(_.invoke(userServiceThis, 'validatePassword', control.value, username)),
477 map((resp: { valid: boolean; credits: number; valuation: string }) => {
478 if (_.isFunction(callback)) {
479 callback(resp.valid, resp.credits, resp.valuation);
484 return { passwordPolicy: true };