Skip to content

Commit

Permalink
feat(json-schema): add error when x-component can not found
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Apr 23, 2021
1 parent ffb52a7 commit 8bc884b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 147 deletions.
28 changes: 22 additions & 6 deletions packages/json-schema/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@ const omitInvalid = (target: any) => {
)
}

const findComponent = (
type: 'component' | 'decorator',
path: Formily.Core.Types.FormPathPattern,
options: ISchemaFieldFactoryOptions,
state: IFieldState
) => {
if (path) {
const component =
FormPath.getIn(options?.components, path) || state?.[type]?.[0]
if (component) {
return component
}
//Todo: need to use __DEV__ keyword
console.error(
`[Formily JSON Schema]: Cannot find the '${path}' component mapped by Schema.x-${type}`
)
}
return state?.[type]?.[0]
}

const getStateFromSchema = (
schema: Schema,
options: ISchemaFieldFactoryOptions,
Expand All @@ -224,15 +244,11 @@ const getStateFromSchema = (
display: schema['x-display'],
pattern: schema['x-pattern'],
decorator: [
(schema['x-decorator'] &&
FormPath.getIn(options?.components, schema['x-decorator'])) ||
state?.decorator?.[0],
findComponent('decorator', schema['x-decorator'], options, state),
schema['x-decorator-props'] || state?.decorator?.[1],
],
component: [
(schema['x-component'] &&
FormPath.getIn(options?.components, schema['x-component'])) ||
state?.component?.[0],
findComponent('component', schema['x-component'], options, state),
schema['x-component-props'] || state?.component?.[1],
],
})
Expand Down
71 changes: 0 additions & 71 deletions packages/shared/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { Subscribable } from '../subscribable'
import { merge } from '../merge'
import { instOf } from '../instanceof'
import { isFn, isHTMLElement, isNumberLike, isReactElement } from '../checkers'
import { log } from '../log'
import { defaults } from '../defaults'

describe('array', () => {
Expand Down Expand Up @@ -525,76 +524,6 @@ describe('types', () => {
})
})

describe('log', () => {
const SomeString = Date.now().toString(32)
const SomeObject = { v: SomeString }
const Keyword = 'Formily'
const Tips = 'you should do something'
const FormilyLog = log
test('log api', () => {
expect(FormilyLog.log(SomeString)).toEqual({
content: SomeString,
keyword: Keyword,
})
expect(FormilyLog.log(SomeObject)).toEqual({
content: SomeObject,
keyword: Keyword,
})
})
test('info api', () => {
expect(FormilyLog.info(SomeString)).toEqual({
content: SomeString,
keyword: Keyword,
})
expect(FormilyLog.info(SomeObject)).toEqual({
content: SomeObject,
keyword: Keyword,
})
})
test('warn api', () => {
expect(FormilyLog.warn(SomeString)).toEqual({
content: SomeString,
keyword: Keyword,
})
expect(FormilyLog.warn(SomeObject)).toEqual({
content: SomeObject,
keyword: Keyword,
})

expect(FormilyLog.warn(SomeString, Tips)).toEqual({
content: SomeString,
keyword: Keyword,
tips: Tips,
})
expect(FormilyLog.warn(SomeObject, Tips)).toEqual({
content: SomeObject,
keyword: Keyword,
tips: Tips,
})
})
test('error api', () => {
expect(FormilyLog.error(SomeString)).toEqual({
content: SomeString,
keyword: Keyword,
})
expect(FormilyLog.error(SomeObject)).toEqual({
content: SomeObject,
keyword: Keyword,
})

expect(FormilyLog.error(SomeString, Tips)).toEqual({
content: SomeString,
keyword: Keyword,
tips: Tips,
})
expect(FormilyLog.error(SomeObject, Tips)).toEqual({
content: SomeObject,
keyword: Keyword,
tips: Tips,
})
})
})

describe('merge', () => {
test('assign', () => {
const target = {
Expand Down
1 change: 0 additions & 1 deletion packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ export * from './subscribable'
export * from './merge'
export * from './instanceof'
export * from './defaults'
export * from './log'
export * from './uid'
69 changes: 0 additions & 69 deletions packages/shared/src/log.ts

This file was deleted.

0 comments on commit 8bc884b

Please sign in to comment.