Skip to content

Commit e859016

Browse files
committed
Update validators to identify JavaScript Date objects (from Material Datepicker) and convert them to strings or numbers when outputting to JSON.
1 parent 69d1757 commit e859016

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/lib/src/shared/validator.functions.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export function isEmpty(value: any): boolean {
175175
* Checks if a value is a string.
176176
*
177177
* @param {any} object - the value to check
178+
* @param {any = false} strict - if truthy, also checks JavaScript tyoe
178179
* @return {boolean} - true if string, false if not
179180
*/
180181
export function isString(value: any): value is string {
@@ -184,7 +185,7 @@ export function isString(value: any): value is string {
184185
/**
185186
* 'isNumber' utility function
186187
*
187-
* Checks if a value is a regular number or a numeric string.
188+
* Checks if a value is a regular number, numeric string, or JavaScript Date.
188189
*
189190
* @param {any} object - the value to check
190191
* @param {any = false} strict - if truthy, also checks JavaScript tyoe
@@ -245,6 +246,11 @@ export function isArray(item: any): boolean {
245246
Object.prototype.toString.call(item) === '[object Array]';
246247
}
247248

249+
export function isDate(item: any): boolean {
250+
return typeof item === 'object' &&
251+
Object.prototype.toString.call(item) === '[object Date]';
252+
}
253+
248254
export function isMap(item: any): boolean {
249255
return typeof item === 'object' &&
250256
Object.prototype.toString.call(item) === '[object Map]';
@@ -300,7 +306,7 @@ export function getType(value: any, strict: any = false): SchemaType {
300306
if (isBoolean(value, 'strict')) { return 'boolean'; }
301307
if (isInteger(value, strict)) { return 'integer'; }
302308
if (isNumber(value, strict)) { return 'number'; }
303-
if (isString(value)) { return 'string'; }
309+
if (isString(value) || (!strict && isDate(value))) { return 'string'; }
304310
return null;
305311
}
306312

@@ -317,7 +323,7 @@ export function getType(value: any, strict: any = false): SchemaType {
317323
export function isType(value: PrimitiveValue, type: SchemaPrimitiveType): boolean {
318324
switch (type) {
319325
case 'string':
320-
return isString(value);
326+
return isString(value) || isDate(value);
321327
case 'number':
322328
return isNumber(value);
323329
case 'integer':
@@ -361,12 +367,16 @@ export function isPrimitive(value: any): boolean {
361367
* If 'strictIntegers' is FALSE (or not set) the type 'integer' is treated
362368
* exactly the same as 'number', and allows decimals.
363369
*
364-
* Examples:
365-
* toJavaScriptType('10', 'number') = 10
366-
* toJavaScriptType('10', 'integer') = 10
367-
* toJavaScriptType('10.5', 'number') = 10.5
370+
* Valid Examples:
371+
* toJavaScriptType('10', 'number' ) = 10 // '10' is a number
372+
* toJavaScriptType('10', 'integer') = 10 // '10' is also an integer
373+
* toJavaScriptType( 10, 'integer') = 10 // 10 is still an integer
374+
* toJavaScriptType( 10, 'string' ) = '10' // 10 can be made into a string
375+
* toJavaScriptType('10.5', 'number' ) = 10.5 // '10.5' is a number
376+
*
377+
* Invalid Examples:
368378
* toJavaScriptType('10.5', 'integer') = null // '10.5' is not an integer
369-
* toJavaScriptType(10.5, 'integer') = null // 10.5 is still not an integer
379+
* toJavaScriptType( 10.5, 'integer') = null // 10.5 is still not an integer
370380
*
371381
* @param {PrimitiveValue} value - value to convert
372382
* @param {SchemaPrimitiveType | SchemaPrimitiveType[]} types - types to convert to
@@ -390,6 +400,15 @@ export function toJavaScriptType(
390400
}
391401
if (inArray('string', types)) {
392402
if (isString(value)) { return value; }
403+
// If value is a date, and types includes 'string',
404+
// convert the date to a string
405+
if (isDate(value)) { return value.toISOString().slice(0, 10); }
406+
if (isNumber(value)) { return value.toString(); }
407+
}
408+
// If value is a date, and types includes 'integer' or 'number',
409+
// but not 'string', convert the date to a number
410+
if (isDate(value) && (inArray('integer', types) || inArray('number', types))) {
411+
return value.getTime();
393412
}
394413
if (inArray('boolean', types)) {
395414
if (isBoolean(value, true)) { return true; }

0 commit comments

Comments
 (0)