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: Record<string, any> = {
94 patternHelpText: typeParams.patternHelpText
97 if (typeParams.isNumberType) {
98 if (configOption.max && configOption.max !== '') {
99 typeValidators['max'] = configOption.max;
100 typeValidators.validators.push(Validators.max(configOption.max));
103 if (configOption.min && configOption.min !== '') {
104 typeValidators['min'] = configOption.min;
105 typeValidators.validators.push(Validators.min(configOption.min));
106 } else if ('defaultMin' in typeParams) {
107 typeValidators['min'] = typeParams.defaultMin;
108 typeValidators.validators.push(Validators.min(typeParams.defaultMin));
111 if (configOption.type === 'float') {
112 typeValidators.validators.push(CdValidators.decimalNumber());
114 typeValidators.validators.push(CdValidators.number(typeParams.allowsNegative));
116 } else if (configOption.type === 'addr') {
117 typeValidators.validators = [CdValidators.ip()];
118 } else if (configOption.type === 'uuid') {
119 typeValidators.validators = [CdValidators.uuid()];
122 return typeValidators;
125 public static getTypeStep(type: string, value: number): number | undefined {
126 const numberTypes = ['uint', 'int', 'size', 'secs'];
128 if (numberTypes.includes(type)) {
132 if (type === 'float') {
133 if (value !== null) {
134 const stringVal = value.toString();
135 if (stringVal.indexOf('.') !== -1) {
136 // Value type float and contains decimal characters
137 const decimal = value.toString().split('.');
138 return Math.pow(10, -decimal[1].length);