Skip to content

Commit b5e7e50

Browse files
committed
Pad 'date-time' inputs, if needed, to mach ISO format.
1 parent 5f0b560 commit b5e7e50

6 files changed

+32
-13
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular2-json-schema-form",
3-
"version": "0.6.0-alpha.2",
3+
"version": "0.6.0-alpha.3",
44
"author": {
55
"name": "David Schnell-Davis",
66
"email": "[email protected]"
@@ -85,7 +85,7 @@
8585
"@angular/platform-browser-dynamic": "^5.0.1",
8686
"@angular/router": "^5.0.1",
8787
"@types/ace": "^0.0.35",
88-
"@types/jasmine": "^2.8.0",
88+
"@types/jasmine": "^2.8.2",
8989
"@types/jasminewd2": "~2.0.2",
9090
"@types/node": "^8.0.52",
9191
"brace": "^0.10.0",

src/lib/src/json-schema-form.service.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ export class JsonSchemaFormService {
120120
case 'date':
121121
return 'Must be a date, like "2000-12-31"'
122122
case 'time':
123-
return 'Must be a time, like "1:59" or "01:59.265"'
123+
return 'Must be a time, like "16:20" or "03:14:15.9265"'
124124
case 'date-time':
125-
return 'Must be a date-time, like "2000-12-31" or "2000-03-14T01:59.265"'
125+
return 'Must be a date-time, like "2000-03-14T01:59" or "2000-03-14T01:59:26.535Z"'
126126
case 'email':
127127
return 'Must be an email address, like "[email protected]"'
128128
case 'hostname':
@@ -466,7 +466,9 @@ export class JsonSchemaFormService {
466466
return Object.keys(errors)
467467
// Hide 'required' error, unless it is the only one
468468
.filter(errorKey => errorKey !== 'required' || Object.keys(errors).length === 1)
469-
.map(errorKey => typeof errorMessages[errorKey] === 'function' ? // If custom error message is a function, return result
469+
.map(errorKey =>
470+
// If custom error message is a function, return function result
471+
typeof errorMessages[errorKey] === 'function' ?
470472
errorMessages[errorKey](errors[errorKey]) :
471473
// If custom error message is a string, replace placeholders and return
472474
typeof errorMessages[errorKey] === 'string' ?

src/lib/src/shared/form-group.functions.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ export function buildFormGroupTemplate(
8383
if (!nodeOptions.has('schemaType')) {
8484
nodeOptions.set('schemaPointer', schemaPointer);
8585
nodeOptions.set('schemaType', schema.type);
86+
if (schema.format) {
87+
nodeOptions.set('schemaFormat', schema.format);
88+
if (!schema.type) { nodeOptions.set('schemaType', 'string'); }
89+
}
8690
if (controlType) {
8791
nodeOptions.set('templatePointer', templatePointer);
8892
nodeOptions.set('templateType', controlType);
@@ -406,8 +410,7 @@ export function formatFormData(
406410
inArray(schemaType, ['string', 'integer', 'number', 'boolean'])
407411
) {
408412
const newValue = (fixErrors || (value === null && returnEmptyFields)) ?
409-
toSchemaType(value, schemaType) :
410-
toJavaScriptType(value, schemaType);
413+
toSchemaType(value, schemaType) : toJavaScriptType(value, schemaType);
411414
if (isDefined(newValue) || returnEmptyFields) {
412415
JsonPointer.set(formattedData, dataPointer, newValue);
413416
}
@@ -425,6 +428,19 @@ export function formatFormData(
425428
}
426429
});
427430
}
431+
// Finish incomplete 'date-time' entries
432+
if (dataMap.get(genericPointer).get('schemaFormat') === 'date-time') {
433+
// "2000-03-14T01:59:26.535" -> "2000-03-14T01:59:26.535Z" (add "Z")
434+
if (/^\d\d\d\d-[0-1]\d-[0-3]\d[t\s][0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?$/i.test(value)) {
435+
JsonPointer.set(formattedData, dataPointer, `${value}Z`);
436+
// "2000-03-14T01:59" -> "2000-03-14T01:59:00Z" (add ":00Z")
437+
} else if (/^\d\d\d\d-[0-1]\d-[0-3]\d[t\s][0-2]\d:[0-5]\d$/i.test(value)) {
438+
JsonPointer.set(formattedData, dataPointer, `${value}:00Z`);
439+
// "2000-03-14" -> "2000-03-14T00:00:00Z" (add "T00:00:00Z")
440+
} else if (fixErrors && /^\d\d\d\d-[0-1]\d-[0-3]\d$/i.test(value)) {
441+
JsonPointer.set(formattedData, dataPointer, `${value}:00:00:00Z`);
442+
}
443+
}
428444
} else if (typeof value !== 'object' || isDate(value) ||
429445
(value === null && returnEmptyFields)
430446
) {

src/lib/src/shared/format-regex.constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const jsonSchemaFormatTests = {
77

88
'time': /^[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:z|[+-]\d\d:\d\d)?$/i,
99

10-
'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,
10+
'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,
1111

1212
// email (sources from jsen validator):
1313
// http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address#answer-8829363

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"inlineSources": true,
88
"declaration": true,
99
"experimentalDecorators": true,
10+
"removeComments": true,
1011
"suppressImplicitAnyIndexErrors": true,
1112
"skipLibCheck": true,
1213
"stripInternal": true,

0 commit comments

Comments
 (0)