63bb072064
* [ML] WIP filter support * [ML] value selector * [ML] only supported filter aggs as options * [ML] WIP apply config * [ML] fix form persistence * [ML] refactor * [ML] support clone * [ML] validation, get es config * [ML] support "exists", fixes for the term form, validation * [ML] fix ts issues * [ML] don't perform request on adding incomplete agg * [ML] basic range number support * [ML] filter bool agg support * [ML] functional tests * [ML] getAggConfigFromEsAgg tests * [ML] fix unit tests * [ML] agg name update on config change, add unit tests * [ML] update snapshot * [ML] range selector enhancements * [ML] improve types * [ML] update step for range selector to support float numbers * [ML] range validation * [ML] term selector improvements * [ML] fix switch between advanced editor * [ML] prefix test ids * [ML] support helper text for aggs item
80 lines
2.2 KiB
TypeScript
80 lines
2.2 KiB
TypeScript
/*
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
*/
|
|
|
|
import { ALLOWED_DATA_UNITS } from '../constants/validation';
|
|
|
|
/**
|
|
* Provides a validator function for maximum allowed input length.
|
|
* @param maxLength Maximum length allowed.
|
|
*/
|
|
export function maxLengthValidator(
|
|
maxLength: number
|
|
): (value: string) => { maxLength: { requiredLength: number; actualLength: number } } | null {
|
|
return (value) =>
|
|
value && value.length > maxLength
|
|
? {
|
|
maxLength: {
|
|
requiredLength: maxLength,
|
|
actualLength: value.length,
|
|
},
|
|
}
|
|
: null;
|
|
}
|
|
|
|
/**
|
|
* Provides a validator function for checking against pattern.
|
|
* @param pattern
|
|
*/
|
|
export function patternValidator(
|
|
pattern: RegExp
|
|
): (value: string) => { pattern: { matchPattern: string } } | null {
|
|
return (value) =>
|
|
pattern.test(value)
|
|
? null
|
|
: {
|
|
pattern: {
|
|
matchPattern: pattern.toString(),
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Composes multiple validators into a single function
|
|
* @param validators
|
|
*/
|
|
export function composeValidators(
|
|
...validators: Array<(value: any) => { [key: string]: any } | null>
|
|
): (value: any) => { [key: string]: any } | null {
|
|
return (value) => {
|
|
const validationResult = validators.reduce((acc, validator) => {
|
|
return {
|
|
...acc,
|
|
...(validator(value) || {}),
|
|
};
|
|
}, {});
|
|
return Object.keys(validationResult).length > 0 ? validationResult : null;
|
|
};
|
|
}
|
|
|
|
export function requiredValidator() {
|
|
return (value: any) => {
|
|
return value === '' || value === undefined || value === null ? { required: true } : null;
|
|
};
|
|
}
|
|
|
|
export type ValidationResult = object | null;
|
|
|
|
export function memoryInputValidator(allowedUnits = ALLOWED_DATA_UNITS) {
|
|
return (value: any) => {
|
|
if (typeof value !== 'string' || value === '') {
|
|
return null;
|
|
}
|
|
const regexp = new RegExp(`\\d+(${allowedUnits.join('|')})$`, 'i');
|
|
return regexp.test(value.trim())
|
|
? null
|
|
: { invalidUnits: { allowedUnits: allowedUnits.join(', ') } };
|
|
};
|
|
}
|