1 import { Validators } from '@angular/forms';
2 import { CdValidators } from '../../../../shared/forms/cd-validators';
3 import { ConfigFormModel } from './configuration-form.model';
5 import * as _ from 'lodash';
7 export class ConfigOptionTypes {
9 private static knownTypes: Array<any> = [
13 humanReadable: 'Unsigned integer value',
15 patternHelpText: 'The entered value needs to be an unsigned number.',
22 humanReadable: 'Integer value',
23 patternHelpText: 'The entered value needs to be a number.',
30 humanReadable: 'Unsigned integer value (>=16bit)',
32 patternHelpText: 'The entered value needs to be a unsigned number.',
39 humanReadable: 'Number of seconds',
41 patternHelpText: 'The entered value needs to be a number >= 1.',
48 humanReadable: 'Double value',
49 patternHelpText: 'The entered value needs to be a number or decimal.',
53 { name: 'str', inputType: 'text', humanReadable: 'Text', isNumberType: false },
57 humanReadable: 'IPv4 or IPv6 address',
58 patternHelpText: 'The entered value needs to be a valid IP address.',
64 humanReadable: 'UUID',
66 'The entered value is not a valid UUID, e.g.: 67dcac9f-2c03-4d6c-b7bd-1210b3a259a8',
69 { name: 'bool', inputType: 'checkbox', humanReadable: 'Boolean value', isNumberType: false }
72 public static getType(type: string): any {
73 const currentType = _.find(this.knownTypes, (t) => {
74 return t.name === type;
77 if (currentType !== undefined) {
81 throw new Error('Found unknown type "' + type + '" for config option.');
84 public static getTypeValidators(configOption: ConfigFormModel): any {
85 const typeParams = ConfigOptionTypes.getType(configOption.type);
87 if (typeParams.name === 'bool' || typeParams.name === 'str') {
91 const typeValidators = { validators: [], patternHelpText: typeParams.patternHelpText };
93 if (typeParams.isNumberType) {
94 if (configOption.max && configOption.max !== '') {
95 typeValidators['max'] = configOption.max;
96 typeValidators.validators.push(Validators.max(configOption.max));
99 if (configOption.min && configOption.min !== '') {
100 typeValidators['min'] = configOption.min;
101 typeValidators.validators.push(Validators.min(configOption.min));
102 } else if ('defaultMin' in typeParams) {
103 typeValidators['min'] = typeParams.defaultMin;
104 typeValidators.validators.push(Validators.min(typeParams.defaultMin));
107 if (configOption.type === 'float') {
108 typeValidators.validators.push(CdValidators.decimalNumber());
110 typeValidators.validators.push(CdValidators.number(typeParams.allowsNegative));
112 } else if (configOption.type === 'addr') {
113 typeValidators.validators = [CdValidators.ip()];
114 } else if (configOption.type === 'uuid') {
115 typeValidators.validators = [CdValidators.uuid()];
118 return typeValidators;