2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 03:29:00 +00:00
books/fyo/model/validationFunction.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

41 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-04-22 11:02:03 +00:00
import { DocValue } from 'fyo/core/types';
import { ValidationError, ValueError } from 'fyo/utils/errors';
import { t } from 'fyo/utils/translation';
import { OptionField } from 'schemas/types';
2022-04-22 11:02:03 +00:00
export function validateEmail(value: DocValue) {
const isValid = /(.+)@(.+){2,}\.(.+){2,}/.test(value as string);
if (!isValid) {
throw new ValidationError(`Invalid email: ${value}`);
}
}
2022-04-22 11:02:03 +00:00
export function validatePhoneNumber(value: DocValue) {
const isValid = /[+]{0,1}[\d ]+/.test(value as string);
if (!isValid) {
throw new ValidationError(`Invalid phone: ${value}`);
}
}
export function validateSelect(field: OptionField, value: string) {
const options = field.options;
if (!options) {
return;
}
if (!field.required && !value) {
return;
}
const validValues = options.map((o) => o.value);
if (validValues.includes(value)) {
return;
}
const labels = options.map((o) => o.label).join(', ');
throw new ValueError(
t`Invalid value ${value} for ${field.label}. Must be one of ${labels}`
);
}