diff --git a/catalog/app/components/JsonEditor/State.js b/catalog/app/components/JsonEditor/State.js index 90bd3d58992..fe08d08eb06 100644 --- a/catalog/app/components/JsonEditor/State.js +++ b/catalog/app/components/JsonEditor/State.js @@ -1,8 +1,9 @@ -import * as dateFns from 'date-fns' import * as FP from 'fp-ts' import * as R from 'ramda' import * as React from 'react' +import * as jsonSchemaUtils from 'utils/json-schema/json-schema' + import { COLUMN_IDS, EMPTY_VALUE } from './constants' const serializeAddress = (addressPath) => `/${addressPath.join('/')}` @@ -109,17 +110,17 @@ function calcReactId(valuePath, value) { } function getDefaultValue(jsonDictItem) { - if (!jsonDictItem || !jsonDictItem.valueSchema) return EMPTY_VALUE - - const schema = jsonDictItem.valueSchema - try { - if (schema.format === 'date' && schema.dateformat) - return dateFns.format(new Date(), schema.dateformat) - } catch (error) { - // eslint-disable-next-line no-console - console.error(error) - } - if (schema.default !== undefined) return schema.default + if (!jsonDictItem?.valueSchema) return EMPTY_VALUE + + const defaultFromSchema = jsonSchemaUtils.getDefaultValue(jsonDictItem?.valueSchema) + if (defaultFromSchema !== undefined) return defaultFromSchema + + // TODO: + // get defaults from nested objects + // const setDefaults = jsonSchemaUtils.makeSchemaDefaultsSetter(jsonDictItem?.valueSchema) + // const nestedDefaultFromSchema = setDefaults() + // if (nestedDefaultFromSchema !== undefined) return nestedDefaultFromSchema + return EMPTY_VALUE } diff --git a/catalog/app/containers/Bucket/PackageDialog/PackageDialog.tsx b/catalog/app/containers/Bucket/PackageDialog/PackageDialog.tsx index 02f1be705c0..18199b62bd5 100644 --- a/catalog/app/containers/Bucket/PackageDialog/PackageDialog.tsx +++ b/catalog/app/containers/Bucket/PackageDialog/PackageDialog.tsx @@ -13,7 +13,11 @@ import * as APIConnector from 'utils/APIConnector' import * as AWS from 'utils/AWS' import * as Sentry from 'utils/Sentry' import { mkFormError } from 'utils/formTools' -import { JsonSchema, makeSchemaValidator } from 'utils/json-schema' +import { + JsonSchema, + makeSchemaDefaultsSetter, + makeSchemaValidator, +} from 'utils/json-schema' import * as packageHandleUtils from 'utils/packageHandle' import * as s3paths from 'utils/s3paths' import * as workflows from 'utils/workflows' @@ -169,7 +173,8 @@ export function mkMetaValidator(schema?: JsonSchema) { } if (schema) { - const errors = schemaValidator(value || {}) + const setDefaults = makeSchemaDefaultsSetter(schema) + const errors = schemaValidator(setDefaults(value || {})) if (errors.length) return errors } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ffa0f95b181..c40dcee3bba 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -34,6 +34,7 @@ * [Added] Edit button for text files in packages ([#3070](https://github.com/quiltdata/quilt/pull/3070)) * [Fixed] Fix package creation in S3 buckets with SSE-KMS enabled ([#2754](https://github.com/quiltdata/quilt/pull/2754)) * [Fixed] Fix creation of packages with large (4+ GiB) files ([#2933](https://github.com/quiltdata/quilt/pull/2933)) +* [Fixed] Fix pre-popullation of default dates when using "dateformat" + {"format": "date"} ([3082](https://github.com/quiltdata/quilt/pull/3082)) * [Changed] Clean up home page ([#2780](https://github.com/quiltdata/quilt/pull/2780)). * [Changed] Make `pkgpush` lambda directly invocable, adjust handling of parameters and errors ([#2776](https://github.com/quiltdata/quilt/pull/2776)) * [Changed] Push packages via GraphQL ([#2768](https://github.com/quiltdata/quilt/pull/2768))