Skip to content

Commit

Permalink
fix(@uform/shared): fix isValid (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Jan 11, 2020
1 parent 406f9fb commit 4136691
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
FormPath.parse(state.path)
)
let val = getValue()
return (index !== undefined ? newPath.concat(index) : newPath).existIn(
return (isValid(index) ? newPath.concat(index) : newPath).existIn(
val,
newPath
)
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/shared/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
globalThisPolyfill,
Subscribable,
FormPath,
FormPathPattern
FormPathPattern,
isValid
} from '@uform/shared'
import produce, { Draft, setAutoFreeze } from 'immer'
import {
Expand Down Expand Up @@ -133,7 +134,7 @@ export const createStateModel = <State = {}, Props = {}>(
)
if (isFn(this.controller.dirtyCheck)) {
const result = this.controller.dirtyCheck(this.dirtys)
if (result !== undefined) {
if (isValid(result)) {
Object.assign(this.dirtys, result)
}
}
Expand Down Expand Up @@ -179,7 +180,7 @@ export const createStateModel = <State = {}, Props = {}>(
)
if (isFn(this.controller.dirtyCheck)) {
const result = this.controller.dirtyCheck(this.dirtys)
if (result !== undefined) {
if (isValid(result)) {
Object.assign(this.dirtys, result)
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/state/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const FieldState = createStateModel<IFieldState, IFieldStateProps>(

readRequired(rules: any[]) {
for (let i = 0; i < rules.length; i++) {
if (rules[i].required !== undefined) {
if (isValid(rules[i].required)) {
return rules[i].required
}
}
Expand All @@ -90,8 +90,8 @@ export const FieldState = createStateModel<IFieldState, IFieldStateProps>(
} else {
rules = rules.reduce((buf: any[], item: any) => {
const keys = Object.keys(item || {})
if (item.required !== undefined) {
if (item.message !== undefined) {
if (isValid(item.required)) {
if (isValid(item.message)) {
if (keys.length > 2) {
return buf.concat({
...item,
Expand Down
8 changes: 4 additions & 4 deletions packages/react-schema-renderer/src/shared/connect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { isArr, each, isFn } from '@uform/shared'
import { isArr, each, isFn,isValid } from '@uform/shared'
import {
ISchema,
IConnectOptions,
Expand Down Expand Up @@ -81,7 +81,7 @@ export const connect = (options?: IConnectOptions) => {
onBlur: () => mutators.blur(),
onFocus: () => mutators.focus()
}
if (editable !== undefined) {
if (isValid(editable)) {
if (isFn(editable)) {
if (!editable(name)) {
componentProps.disabled = true
Expand All @@ -105,7 +105,7 @@ export const connect = (options?: IConnectOptions) => {

if (isFn(options.getProps)) {
const newProps = options.getProps(componentProps, fieldProps)
if (newProps !== undefined) {
if (isValid(newProps)) {
componentProps = newProps as any
}
}
Expand All @@ -114,7 +114,7 @@ export const connect = (options?: IConnectOptions) => {
componentProps.dataSource = createEnum((props as ISchema).enum)
}

if (componentProps.editable !== undefined) {
if (isValid(componentProps.editable)) {
delete componentProps.editable
}

Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isFn, FormPath, Subscribable } from '@uform/shared'
import { isFn, FormPath, Subscribable, isValid } from '@uform/shared'
import {
IFormEffect,
IFormActions,
Expand Down Expand Up @@ -93,11 +93,11 @@ export const getValueFromEvent = (event: any) => {
if (
!isReactNative &&
event.nativeEvent &&
event.nativeEvent.text !== undefined
isValid(event.nativeEvent.text)
) {
return event.nativeEvent.text
}
if (isReactNative && event.nativeEvent !== undefined) {
if (isReactNative && isValid(event.nativeEvent)) {
return event.nativeEvent.text
}

Expand Down
3 changes: 2 additions & 1 deletion packages/validator/src/rules.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getMessage } from './message'
import {
isEmpty,
isValid,
stringLength,
isStr,
isArr,
Expand All @@ -12,7 +13,7 @@ import { ValidateDescription } from './types'
const isValidateEmpty = (value: any) => {
if (isArr(value)) {
for (let i = 0; i < value.length; i++) {
if (value[i] !== undefined) return false
if (isValid(value[i])) return false
}
return true
} else {
Expand Down
8 changes: 5 additions & 3 deletions packages/validator/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isStr,
isArr,
isObj,
isValid,
each,
FormPath,
FormPathPattern
Expand Down Expand Up @@ -101,8 +102,9 @@ class FormValidator {
errors: string[]
warnings: string[]
}> {
const first =
options.first !== undefined ? !!options.first : !!this.validateFirst
const first = isValid(options.first)
? !!options.first
: !!this.validateFirst
const errors: string[] = []
const warnings = []
try {
Expand All @@ -113,7 +115,7 @@ class FormValidator {
)
for (let l = 0; l < keys.length; l++) {
let key = keys[l]
if (ruleObj.hasOwnProperty(key) && ruleObj[key] !== undefined) {
if (ruleObj.hasOwnProperty(key) && isValid(ruleObj[key])) {
const rule = ValidatorRules[key]
if (rule) {
const payload = await rule(value, ruleObj)
Expand Down

0 comments on commit 4136691

Please sign in to comment.