mirror of
https://github.com/ansible/awx.git
synced 2026-01-23 07:28:02 -03:30
82 lines
1.8 KiB
JavaScript
82 lines
1.8 KiB
JavaScript
import { t } from '@lingui/macro';
|
|
|
|
export function required(message, i18n) {
|
|
const errorMessage = message || i18n._(t`This field must not be blank`);
|
|
return value => {
|
|
if (typeof value === 'string' && !value.trim()) {
|
|
return errorMessage;
|
|
}
|
|
if (typeof value === 'number' && !Number.isNaN(value)) {
|
|
return undefined;
|
|
}
|
|
if (!value) {
|
|
return errorMessage;
|
|
}
|
|
return undefined;
|
|
};
|
|
}
|
|
|
|
export function maxLength(max, i18n) {
|
|
return value => {
|
|
if (value.trim().length > max) {
|
|
return i18n._(t`This field must not exceed ${max} characters`);
|
|
}
|
|
return undefined;
|
|
};
|
|
}
|
|
|
|
export function minMaxValue(min, max, i18n) {
|
|
return value => {
|
|
if (value < min || value > max) {
|
|
return i18n._(
|
|
t`This field must be a number and have a value between ${min} and ${max}`
|
|
);
|
|
}
|
|
return undefined;
|
|
};
|
|
}
|
|
|
|
export function defaultIsNotAvailable(choices, i18n) {
|
|
return defaultValue => {
|
|
if (!choices.includes(defaultValue)) {
|
|
return i18n._(
|
|
t`Default choice must be answered from the choices listed.`
|
|
);
|
|
}
|
|
return undefined;
|
|
};
|
|
}
|
|
|
|
export function requiredEmail(i18n) {
|
|
return value => {
|
|
if (!value) {
|
|
return i18n._(t`This field must not be blank`);
|
|
}
|
|
if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)) {
|
|
return i18n._(t`Invalid email address`);
|
|
}
|
|
return undefined;
|
|
};
|
|
}
|
|
|
|
export function noWhiteSpace(i18n) {
|
|
return value => {
|
|
if (/\s/.test(value)) {
|
|
return i18n._(t`This field must not contain spaces`);
|
|
}
|
|
return undefined;
|
|
};
|
|
}
|
|
|
|
export function combine(validators) {
|
|
return value => {
|
|
for (let i = 0; i < validators.length; i++) {
|
|
const error = validators[i](value);
|
|
if (error) {
|
|
return error;
|
|
}
|
|
}
|
|
return undefined;
|
|
};
|
|
}
|