Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepopulate date #2121

Merged
merged 16 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions catalog/app/components/JsonEditor/State.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as dateFns from 'date-fns'
import * as R from 'ramda'
import * as React from 'react'

Expand Down Expand Up @@ -141,8 +142,17 @@ function calcReactId(valuePath, value) {

function getDefaultValue(jsonDictItem) {
if (!jsonDictItem || !jsonDictItem.valueSchema) return EMPTY_VALUE
if (jsonDictItem.valueSchema.default === undefined) return EMPTY_VALUE
return jsonDictItem.valueSchema.default

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
return EMPTY_VALUE
}

function getJsonDictItem(jsonDict, obj, parentPath, key, sortOrder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ describe('containers/Bucket/PackageDialog/PackageDialog', () => {

describe('mkMetaValidator', () => {
test('should return no error when no metadata', () => {
expect(PD.mkMetaValidator(null)(null)).toBeUndefined()
expect(PD.mkMetaValidator()(null)).toBeUndefined()
})

test('should return error when metadata is not an object', () => {
// TODO: remove this test when all references will be in typescript
// @ts-expect-error
expect(PD.mkMetaValidator(null)(123)).toMatchObject({
expect(PD.mkMetaValidator()(123)).toMatchObject({
message: 'Metadata must be a valid JSON object',
})
})

test('should return no error when no Schema and metadata is object', () => {
expect(PD.mkMetaValidator(null)({ any: 'thing' })).toBeUndefined()
expect(PD.mkMetaValidator()({ any: 'thing' })).toBeUndefined()
})

test("should return error when metadata isn't compliant with Schema", () => {
Expand Down
14 changes: 8 additions & 6 deletions catalog/app/containers/Bucket/PackageDialog/PackageDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import AsyncResult from 'utils/AsyncResult'
import * as APIConnector from 'utils/APIConnector'
import * as AWS from 'utils/AWS'
import useDragging from 'utils/dragging'
import { makeSchemaDefaultsSetter, makeSchemaValidator } from 'utils/json-schema'
import {
JsonSchema,
makeSchemaDefaultsSetter,
makeSchemaValidator,
} from 'utils/json-schema'
import pipeThru from 'utils/pipeThru'
import * as spreadsheets from 'utils/spreadsheets'
import { readableBytes } from 'utils/string'
Expand Down Expand Up @@ -100,8 +104,6 @@ const readTextFile = (file: File): Promise<string> =>
reader.readAsText(file)
})

type JsonSchema = $TSFixMe

const readFile = (file: File, schema?: JsonSchema): Promise<string | {}> => {
const mimeType = mime.extension(file.type)
if (mimeType && /ods|odt|csv|xlsx|xls/.test(mimeType))
Expand Down Expand Up @@ -182,7 +184,7 @@ export function useNameExistence(bucket: string) {
return React.useMemo(() => ({ validate, inc }), [validate, inc])
}

export function mkMetaValidator(schema: object | null) {
export function mkMetaValidator(schema?: JsonSchema) {
// TODO: move schema validation to utils/validators
// but don't forget that validation depends on library.
// Maybe we should split validators to files at first
Expand All @@ -205,7 +207,7 @@ export function mkMetaValidator(schema: object | null) {
}
}

export const getMetaValue = (value: unknown, optSchema: unknown) =>
export const getMetaValue = (value: unknown, optSchema: JsonSchema) =>
value
? pipeThru(value || {})(
makeSchemaDefaultsSetter(optSchema),
Expand Down Expand Up @@ -690,7 +692,7 @@ export function SchemaFetcher({
AsyncResult.Ok({
...defaultProps,
responseError,
validate: mkMetaValidator(null),
validate: mkMetaValidator(),
}),
_: () => AsyncResult.Ok({ ...defaultProps, schemaLoading: true }),
}),
Expand Down
136 changes: 0 additions & 136 deletions catalog/app/utils/json-schema/json-schema.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,6 @@ describe('utils/json-schema', () => {
})
})

it('should ignore default value from Schema, when value set', () => {
expect(setDefaults({ a: 123 })).toMatchObject({
a: 123,
})
})

it('should set default value from Schema, when nested value set', () => {
expect(setDefaults({ optList: [{}] })).toMatchObject({
optList: [
Expand All @@ -287,5 +281,100 @@ describe('utils/json-schema', () => {
],
})
})

it('should return the same value if no schema', () => {
const obj = { a: 1 }
expect(makeSchemaDefaultsSetter()(obj)).toBe(obj)
})

it('should return the same value if no properties schema', () => {
const obj = { a: 1 }
const schema = { type: 'array', items: { type: 'number' } }
expect(makeSchemaDefaultsSetter(schema)(obj)).toBe(obj)
})

it('should return value with defaults', () => {
const obj = { a: { b: 1 } }
const schema = {
type: 'object',
properties: {
a: {
type: 'object',
properties: {
b: { type: 'string', default: 'User set it' },
c: { type: 'number', default: 123 },
d: {
type: 'object',
properties: {
e: {
type: 'object',
properties: {
f: { type: 'number', default: 456 },
},
},
},
},
},
},
g: { type: 'number', default: 789 },
},
}
expect(makeSchemaDefaultsSetter(schema)(obj)).toMatchObject({
a: {
b: 1,
c: 123,
d: {
e: {
f: 456,
},
},
},
g: 789,
})
})

it('should return value with prepopulated date', () => {
jest.useFakeTimers('modern')
jest.setSystemTime(new Date(2020, 0, 30))

const obj = { a: { b: 1 } }
const schema = {
type: 'object',
properties: {
a: {
type: 'object',
properties: {
b: { type: 'string', format: 'date', dateformat: 'yyyy-MM-dd' },
c: { type: 'string', format: 'date', dateformat: 'yyyy-MM-dd' },
d: {
type: 'object',
properties: {
e: {
type: 'object',
properties: {
f: { type: 'string', format: 'date', dateformat: 'yyyy-MM-dd' },
},
},
},
},
},
},
g: { type: 'string', format: 'date', dateformat: 'yyyy-MM-dd' },
},
}
expect(makeSchemaDefaultsSetter(schema)(obj)).toMatchObject({
a: {
b: 1,
c: '2020-01-30',
d: {
e: {
f: '2020-01-30',
},
},
},
g: '2020-01-30',
})
jest.useRealTimers()
})
})
})
Loading