File tree Expand file tree Collapse file tree 2 files changed +11
-7
lines changed Expand file tree Collapse file tree 2 files changed +11
-7
lines changed Original file line number Diff line number Diff line change 1
- // AJV fast format regular expressions from :
1
+ // updated from AJV fast format regular expressions:
2
2
// https://github.com/epoberezkin/ajv/blob/master/lib/compile/formats.js
3
3
4
4
export const jsonSchemaFormatTests = {
@@ -7,6 +7,8 @@ export const jsonSchemaFormatTests = {
7
7
8
8
'time' : / ^ [ 0 - 2 ] \d : [ 0 - 5 ] \d : [ 0 - 5 ] \d (?: \. \d + ) ? (?: z | [ + - ] \d \d : \d \d ) ? $ / i,
9
9
10
+ // Modified to allow incomplete entries, such as
11
+ // "2000-03-14T01:59:26.535" (needs "Z") or "2000-03-14T01:59" (needs ":00Z")
10
12
'date-time' : / ^ \d \d \d \d - [ 0 - 1 ] \d - [ 0 - 3 ] \d [ t \s ] [ 0 - 2 ] \d : [ 0 - 5 ] \d (?: : [ 0 - 5 ] \d ) ? (?: \. \d + ) ? (?: z | [ + - ] \d \d : \d \d ) ? $ / i,
11
13
12
14
// email (sources from jsen validator):
Original file line number Diff line number Diff line change @@ -331,19 +331,21 @@ export class JsonValidators {
331
331
return ( control : AbstractControl , invert = false ) : ValidationErrors | null => {
332
332
if ( isEmpty ( control . value ) ) { return null ; }
333
333
let isValid : boolean ;
334
- let currentValue : string = control . value ;
335
- if ( ! isString ( currentValue ) ) {
336
- isValid = false ;
337
- } else {
334
+ let currentValue : string | Date = control . value ;
335
+ if ( isString ( currentValue ) ) {
338
336
const formatTest : Function | RegExp = jsonSchemaFormatTests [ requiredFormat ] ;
339
337
if ( typeof formatTest === 'object' ) {
340
- isValid = ( < RegExp > formatTest ) . test ( currentValue ) ;
338
+ isValid = ( < RegExp > formatTest ) . test ( < string > currentValue ) ;
341
339
} else if ( typeof formatTest === 'function' ) {
342
- isValid = ( < Function > formatTest ) ( currentValue ) ;
340
+ isValid = ( < Function > formatTest ) ( < string > currentValue ) ;
343
341
} else {
344
342
console . error ( `format validator error: "${ requiredFormat } " is not a recognized format.` ) ;
345
343
isValid = true ;
346
344
}
345
+ } else {
346
+ // Allow JavaScript Date objects
347
+ isValid = [ 'date' , 'time' , 'date-time' ] . includes ( requiredFormat ) &&
348
+ Object . prototype . toString . call ( currentValue ) === '[object Date]' ;
347
349
}
348
350
return xor ( isValid , invert ) ?
349
351
null : { 'format' : { requiredFormat, currentValue } } ;
You can’t perform that action at this time.
0 commit comments