Skip to content

Commit

Permalink
feat: add ajvOptions prop to customize ajv configurationn (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
mengshang918 committed Mar 8, 2022
1 parent 3ec8449 commit cfe27e9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/ajv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { Plugin, Options } from 'ajv/dist/2019'
/**
* 生成ajv示例,并加载drip-form官方插件
*/
function registerAjv(): Ajv {
function registerAjv(options?: Options): Ajv {
// 默认使用草案2019
const ajv = new Ajv2019({
// 不允许type:['string','number']等联合模式 推荐使用anyOf代替
Expand All @@ -32,6 +32,7 @@ function registerAjv(): Ajv {
discriminator: true,
// 是否删除数据中Schema未定义的字段
removeAdditional: false,
...options,
})
// 支持草案7
ajv.addMetaSchema(draft7MetaSchema, 'http://json-schema.org/draft-07/schema#')
Expand Down
18 changes: 10 additions & 8 deletions packages/drip-form/src/DripForm/DripFormContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ import React, {
memo,
} from 'react'
import DripForm from '..'
import dripAjv from '@jdfed/ajv'
import type { DripFormProps } from '../type'
import type { Theme } from '@jdfed/utils'
import dripAjv from '@jdfed/ajv'
import type Ajv from 'ajv/dist/2019'

const { registerAjv, loadAjvPlugins } = dripAjv

type State = {
hasError: boolean
errorTips: string
ajv: Ajv
}

const ajv = registerAjv()
class ErrorBoundary extends PureComponent<
DripFormProps & { formRef: React.Ref<any> },
State
> {
constructor(props: DripFormProps & { formRef: React.Ref<any> }) {
super(props)
const { uiComponents, unitedSchema, plugins } = props

const { uiComponents, unitedSchema, plugins, ajvOptions } = props
const initError = {
error: false,
tips: '发生错误,请联系管理员!',
Expand All @@ -38,15 +39,16 @@ class ErrorBoundary extends PureComponent<
initError.error = true
initError.tips = '请确认导入的主题组件与uiSchema中配置的一致!'
}

const ajv = registerAjv(ajvOptions)
//加载外部ajv插件
loadAjvPlugins(ajv, plugins)
this.state = {
// 内部组件是否有错误
hasError: initError.error,
// 错误提示
errorTips: initError.tips,
ajv,
}
//加载外部ajv插件
loadAjvPlugins(ajv, plugins)
}

static getDerivedStateFromError(): { hasError: true } {
Expand All @@ -60,7 +62,7 @@ class ErrorBoundary extends PureComponent<

render(): ReactNode {
const style: CSSProperties = { color: 'red' }
const { hasError, errorTips } = this.state
const { hasError, errorTips, ajv } = this.state

return hasError ? (
<div style={style}>{errorTips}</div>
Expand Down
1 change: 1 addition & 0 deletions packages/drip-form/src/DripForm/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export type DripFormProps = {
containerHoc?: ContainerHoc
onSubmit?: FooterBtnFuncType
onCancel?: FooterBtnFuncType
ajvOptions?: Options
}

/**
Expand Down

0 comments on commit cfe27e9

Please sign in to comment.