-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrequired.ts
More file actions
27 lines (23 loc) · 932 Bytes
/
required.ts
File metadata and controls
27 lines (23 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { FieldValidationResult, FieldValidationFunction } from '../entities';
export interface RequiredParams {
trim: boolean;
}
const DEFAULT_PARAMS: RequiredParams = { trim: true };
export const required: FieldValidationFunction = (value, vm, customParams: RequiredParams = DEFAULT_PARAMS) => {
const validationResult = new FieldValidationResult();
const isValid = isValidField(value, Boolean(customParams.trim));
validationResult.errorMessage = isValid ? '' : 'Please fill in this mandatory field.';
validationResult.succeeded = isValid;
validationResult.type = 'REQUIRED';
return validationResult;
}
function isValidField(value, trim: boolean): boolean {
return typeof value === 'string' ?
isStringValid(value, trim) :
value === true || typeof value === "number";
}
function isStringValid(value: string, trim: boolean): boolean {
return trim ?
value.trim().length > 0 :
value.length > 0;
}