1 import { Validators } from '@angular/forms';
3 import * as _ from 'lodash';
5 import { CdValidators } from '../../forms/cd-validators';
6 import { ConfigFormModel } from './config-option.model';
8 export class ConfigOptionTypes {
10 private static knownTypes: Array<any> = [
14 humanReadable: 'Unsigned integer value',
16 patternHelpText: 'The entered value needs to be an unsigned number.',
23 humanReadable: 'Integer value',
24 patternHelpText: 'The entered value needs to be a number.',
31 humanReadable: 'Unsigned integer value (>=16bit)',
33 patternHelpText: 'The entered value needs to be a unsigned number.',
40 humanReadable: 'Number of seconds',
42 patternHelpText: 'The entered value needs to be a number >= 1.',
49 humanReadable: 'Double value',
50 patternHelpText: 'The entered value needs to be a number or decimal.',
54 { name: 'str', inputType: 'text', humanReadable: 'Text', isNumberType: false },
58 humanReadable: 'IPv4 or IPv6 address',
59 patternHelpText: 'The entered value needs to be a valid IP address.',
65 humanReadable: 'UUID',
67 'The entered value is not a valid UUID, e.g.: 67dcac9f-2c03-4d6c-b7bd-1210b3a259a8',
70 { name: 'bool', inputType: 'checkbox', humanReadable: 'Boolean value', isNumberType: false }
73 public static getType(type: string): any {
74 const currentType = _.find(this.knownTypes, (t) => {
75 return t.name === type;
78 if (currentType !== undefined) {
82 throw new Error('Found unknown type "' + type + '" for config option.');
85 public static getTypeValidators(configOption: ConfigFormModel): any {
86 const typeParams = ConfigOptionTypes.getType(configOption.type);
88 if (typeParams.name === 'bool' || typeParams.name === 'str') {
92 const typeValidators = { validators: [], patternHelpText: typeParams.patternHelpText };
94 if (typeParams.isNumberType) {
95 if (configOption.max && configOption.max !== '') {
96 typeValidators['max'] = configOption.max;
97 typeValidators.validators.push(Validators.max(configOption.max));
100 if (configOption.min && configOption.min !== '') {
101 typeValidators['min'] = configOption.min;
102 typeValidators.validators.push(Validators.min(configOption.min));
103 } else if ('defaultMin' in typeParams) {
104 typeValidators['min'] = typeParams.defaultMin;
105 typeValidators.validators.push(Validators.min(typeParams.defaultMin));
108 if (configOption.type === 'float') {
109 typeValidators.validators.push(CdValidators.decimalNumber());
111 typeValidators.validators.push(CdValidators.number(typeParams.allowsNegative));
113 } else if (configOption.type === 'addr') {
114 typeValidators.validators = [CdValidators.ip()];
115 } else if (configOption.type === 'uuid') {
116 typeValidators.validators = [CdValidators.uuid()];
119 return typeValidators;
122 public static getTypeStep(type: string, value: number): number | undefined {
123 const numberTypes = ['uint', 'int', 'size', 'secs'];
125 if (numberTypes.includes(type)) {
129 if (type === 'float') {
130 if (value !== null) {
131 const stringVal = value.toString();
132 if (stringVal.indexOf('.') !== -1) {
133 // Value type float and contains decimal characters
134 const decimal = value.toString().split('.');
135 return Math.pow(10, -decimal[1].length);