-
Notifications
You must be signed in to change notification settings - Fork 50
/
validate.ts
153 lines (119 loc) · 4.57 KB
/
validate.ts
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import * as Models from '@nakedobjects/restful-objects';
import * as momentNs from 'moment';
import { ILocalFilter } from '@nakedobjects/services';
import * as Msg from './user-messages';
const moment = momentNs;
export const supportedDateFormats = ['D/M/YYYY', 'D/M/YY', 'D MMM YYYY', 'D MMMM YYYY', 'D MMM YY', 'D MMMM YY'];
export const fixedDateFormat = 'YYYY-MM-DD';
function isInteger(value: number) {
return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
}
function getDate(val: string): Date | null {
const dt1 = moment(val, fixedDateFormat, true);
return dt1.isValid() ? dt1.toDate() : null;
}
export function validateNumber(model: Models.IHasExtensions, newValue: number, filter: ILocalFilter): string {
const format = model.extensions().format();
switch (format) {
case ('int'):
if (!isInteger(newValue)) {
return 'Not an integer';
}
}
const range = model.extensions().range();
if (range) {
const min = range.min;
const max = range.max;
if (min && newValue < min) {
return Msg.outOfRange(newValue, min, max, filter);
}
if (max && newValue > max) {
return Msg.outOfRange(newValue, min, max, filter);
}
}
return '';
}
export function validateStringFormat(model: Models.IHasExtensions, newValue: string): string {
const maxLength = model.extensions().maxLength();
const pattern = model.extensions().pattern();
const len = newValue ? newValue.length : 0;
if (maxLength && len > maxLength) {
return Msg.tooLong;
}
if (pattern) {
const regex = new RegExp(pattern);
return regex.test(newValue) ? '' : Msg.noPatternMatch;
}
return '';
}
export function validateDateTimeFormat(model: Models.IHasExtensions, newValue: Date): string {
return '';
}
export function validateDateFormat(model: Models.IHasExtensions, newValue: Date | string, filter: ILocalFilter): string {
const range = model.extensions().range();
const newDate = (newValue instanceof Date) ? newValue : getDate(newValue);
if (range && newDate) {
const min = range.min ? getDate(range.min as string) : null;
const max = range.max ? getDate(range.max as string) : null;
if (min && newDate < min) {
return Msg.outOfRange(Models.toDateString(newDate), Models.getUtcDate(range.min as string), Models.getUtcDate(range.max as string), filter);
}
if (max && newDate > max) {
return Msg.outOfRange(Models.toDateString(newDate), Models.getUtcDate(range.min as string), Models.getUtcDate(range.max as string), filter);
}
}
return '';
}
export function validateTimeFormat(model: Models.IHasExtensions, newValue: Date): string {
return '';
}
export function validateString(model: Models.IHasExtensions, newValue: any, filter: ILocalFilter): string {
const format = model.extensions().format();
switch (format) {
case ('string'):
return validateStringFormat(model, newValue as string);
case ('date-time'):
return validateDateTimeFormat(model, newValue as Date);
case ('date'):
return validateDateFormat(model, newValue as Date | string, filter);
case ('time'):
return validateTimeFormat(model, newValue as Date);
default:
return '';
}
}
export function validateMandatory(model: Models.IHasExtensions, viewValue: string): string {
// first check
const isMandatory = !model.extensions().optional();
if (isMandatory && (viewValue === '' || viewValue == null)) {
return Msg.mandatory;
}
return '';
}
export function validateMandatoryAgainstType(model: Models.IHasExtensions, viewValue: string, filter: ILocalFilter): string {
// check type
const returnType = model.extensions().returnType();
switch (returnType) {
case ('number'):
const valueAsNumber = parseFloat(viewValue);
if (Number.isFinite(valueAsNumber)) {
return validateNumber(model, valueAsNumber, filter);
}
return Msg.notANumber;
case ('string'):
return validateString(model, viewValue, filter);
case ('boolean'):
return '';
default:
return '';
}
}
export function validateDate(newValue: string, validInputFormats: string[]) {
for (const f of validInputFormats) {
const dt = moment.utc(newValue, f, true);
if (dt.isValid()) {
return dt;
}
}
return null;
}