Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions dumi/docs/concepts/state/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface IState<V> {
error: ValidationError
/** The state's own error info, regardless of child states. */
ownError: ValidationError
/** The state's validation result, is the same as the return value of the validator, regardless of child states. */
/** The state's validation result, regardless of child states. */
rawError: ValidationResult
/** Append validator(s). */
withValidator(...validators: Array<Validator<V>>): this
Expand Down Expand Up @@ -101,7 +101,7 @@ States will not be auto-validated until it becomes **activated**. And they will

### Raw Error

The state's validation result, is the same as the return value of the `validator`, regardless of child states. The difference compared to `ownError` is that it contains the type of `ValidationErrorObject`. You can check details about them in issue [#82](https://github.com/qiniu/formstate-x/issues/82).
The state's validation result, regardless of child states. The difference compared to `ownError` is that it contains the type of `ValidationErrorObject`. You can check details about them in issue [#82](https://github.com/qiniu/formstate-x/issues/82).

### Disable State

Expand Down
1 change: 0 additions & 1 deletion src/adapter/v2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,6 @@ describe('toV2', () => {
}
value: V
touched = false
ownError = undefined
error = undefined
rawError = undefined
activated = false
Expand Down
5 changes: 2 additions & 3 deletions src/adapter/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ class Upgrader<T extends v2.ComposibleValidatable<unknown, V>, V> extends BaseSt

@computed get value() { return this.stateV2.value }
@computed get touched() { return this.stateV2.dirty }
@computed get ownError() {
@computed get rawError() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多了个空格?以及第 30 行也多了个

return getV3OwnError(this.stateV2)
}
@computed get rawError() { return this.ownError }
}
@computed get error() { return this.stateV2.error }
@computed get activated() { return this.stateV2._activated }
@computed get validateStatus() {
Expand Down
22 changes: 9 additions & 13 deletions src/debouncedState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { action, computed, makeObservable, observable, override, reaction } from
import { FieldState } from './fieldState'
import { ValidatableState } from './state'
import { IState, ValidateStatus, ValueOf } from './types'
import { debounce, isPassed, normalizeError } from './utils'
import { debounce } from './utils'

const defaultDelay = 200 // ms

/** Infomation synced from original state */
type OriginalInfo<V> = Pick<IState<V>, 'activated' | 'touched' | 'error' | 'ownError' | 'hasError'>
/** Information synced from original state */
type OriginalInfo<V> = Pick<IState<V>, 'activated' | 'touched' | 'error' | 'ownError' | 'hasError' | 'rawError'>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ownErrorhasError 都可以干掉了


/**
* The state for debounce purpose.
Expand All @@ -24,7 +24,7 @@ export class DebouncedState<S extends IState<V>, V = ValueOf<S>> extends Validat
/** Debounced version of original value */
@observable.ref value!: V

/** Orignal information, same version with current `value` */
/** Original information, same version with current `value` */
@observable.ref private synced!: OriginalInfo<V>

/** Original information for current `value` */
Expand All @@ -43,18 +43,19 @@ export class DebouncedState<S extends IState<V>, V = ValueOf<S>> extends Validat
touched: this.$.touched,
error: this.$.error,
ownError: this.$.ownError,
hasError: this.$.hasError
hasError: this.$.hasError,
rawError: this.$.rawError
}
}

@computed get touched() {
return this.original.touched
}

@override override get ownError() {
@override override get rawError() {
if (this.disabled) return undefined
if (this.rawError) return normalizeError(this.rawError)
return this.original.ownError
if (this.validationResult) return this.validationResult
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!isPassed(this.validationResult)) 会比 if (this.validationResult) 更严谨吗?

return this.original.rawError
}

@override override get error() {
Expand All @@ -63,11 +64,6 @@ export class DebouncedState<S extends IState<V>, V = ValueOf<S>> extends Validat
return this.original.error
}

@override override get hasError() {
if (this.disabled) return false
return !isPassed(this.rawError) || this.original.hasError
}

@override override get validateStatus() {
if (this.disabled) return ValidateStatus.WontValidate
if (
Expand Down
16 changes: 0 additions & 16 deletions src/formState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { observable, computed, isObservable, action, reaction, makeObservable, override } from 'mobx'
import { IState, ValidateStatus, ValidateResult, ValueOfStatesObject } from './types'
import { ValidatableState } from './state'
import { isPassed } from './utils'

abstract class AbstractFormState<T, V> extends ValidatableState<V> implements IState<V> {

Expand Down Expand Up @@ -48,21 +47,6 @@ abstract class AbstractFormState<T, V> extends ValidatableState<V> implements IS
}
}

@override override get hasError() {
if (this.disabled) {
return false
}
if (!isPassed(this.rawError)) {
return true
}
for (const state of this.childStates) {
if (state.hasError) {
return true
}
}
return false
}

/** If reference of child states has been touched. */
@observable protected ownTouched = false

Expand Down
28 changes: 14 additions & 14 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { action, autorun, computed, makeObservable, observable, when } from 'mobx'
import { ValidationResult, ValidationError, IState, Validation, ValidateResult, ValidateStatus, Validator } from './types'
import { ValidationResult, IState, Validation, ValidateResult, ValidationError, ValidateStatus, Validator } from './types'
import Disposable from './disposable'
import { applyValidators, isPromiseLike, isPassed, normalizeError } from './utils'
import { applyValidators, isPromiseLike, normalizeError } from './utils'

/** Extraction for some basic features of State */
export abstract class BaseState extends Disposable implements Pick<
IState, 'rawError' | 'ownError' | 'hasOwnError' | 'hasError' | 'validateStatus' | 'validating' | 'validated'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

弱问这里的 'error' 不用留着吗?

> {
> {

abstract rawError: ValidationResult
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rawErrorValidationResult 是对的吗?
后面还有类似的这种

Copy link
Contributor Author

@Luncher Luncher May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rawError 对 ValidationResult 是对的吗?

是预期的

后面还有类似的这种

啥?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没啥,是预期的就好
我只是看到 error 对 result 感觉怪怪的


abstract error: ValidationError

@computed get hasError() {
return !isPassed(this.rawError)
return !!this.error
}

abstract ownError: ValidationError
@computed get ownError() {
return normalizeError(this.rawError)
}

@computed get hasOwnError() {
return !!this.ownError
Expand Down Expand Up @@ -54,16 +58,12 @@ export abstract class ValidatableState<V> extends BaseState implements IState<V>
@observable activated = false

/**
* The original return value of validation.
* The original validation result.
*/
@observable private _error: ValidationResult
@observable protected validationResult: ValidationResult

@computed get rawError() {
return this.disabled ? undefined : this._error
}

@computed get ownError() {
return normalizeError(this.rawError)
return this.disabled ? undefined : this.validationResult
}

@computed get error() {
Expand All @@ -74,7 +74,7 @@ export abstract class ValidatableState<V> extends BaseState implements IState<V>
* Set validation result.
*/
@action setError(error: ValidationResult) {
this._error = error
this.validationResult = error
}

/** List of validator functions. */
Expand Down Expand Up @@ -167,7 +167,7 @@ export abstract class ValidatableState<V> extends BaseState implements IState<V>
@action reset() {
this.activated = false
this._validateStatus = ValidateStatus.NotValidated
this._error = undefined
this.validationResult = undefined
this.validation = undefined
}

Expand Down
10 changes: 1 addition & 9 deletions src/transformedState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, override } from 'mobx'
import { computed } from 'mobx'
import { BaseState } from './state'
import { IState, Validator, ValueOf } from './types'

Expand All @@ -23,18 +23,10 @@ export class TransformedState<S extends IState<$V>, V, $V = ValueOf<S>> extends
return this.parseOriginalValue(this.$.value)
}

@computed get ownError() {
return this.$.ownError
}

@computed get rawError() {
return this.$.rawError
}

@override override get hasError() {
return this.$.hasError
}

@computed get error() {
return this.$.error
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface IState<V = unknown> {
hasError: boolean
/** The state's own error info, regardless of child states. */
ownError: ValidationError
/** The state's validation result, is the same as the return value of the validator, regardless of child states. */
/** The state's validation result, regardless of child states. */
rawError: ValidationResult
/** If the state contains its own error info. */
hasOwnError: boolean
Expand Down
3 changes: 2 additions & 1 deletion src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe('isErrorObject', () => {
expect(isErrorObject('foo')).toBe(false)
expect(isErrorObject({})).toBe(false)
expect(isErrorObject({ foo: 'foo' })).toBe(false)
expect(isErrorObject({ message: '' })).toBe(true)
expect(isErrorObject({ message: 'msg' })).toBe(true)
expect(isErrorObject({ message: 'msg', extra: 'ext' })).toBe(true)
expect(isErrorObject(new Error('error msg'))).toBe(true)
Expand All @@ -106,7 +107,6 @@ describe('isErrorObject', () => {

class Bar extends Foo {}
expect(isErrorObject(new Bar())).toBe(true)
expect(() => isErrorObject({ message: '' })).toThrow(inValidErrorObjectMsg)
})
})

Expand All @@ -115,6 +115,7 @@ describe('normalizeValidationResult', () => {
expect(normalizeError(undefined)).toBe(undefined)
expect(normalizeError('')).toBe(undefined)
expect(normalizeError('foo')).toBe('foo')
expect(() => normalizeError({ message: '' })).toThrow(inValidErrorObjectMsg)
expect(normalizeError({ message: 'mewo' })).toBe('mewo')
expect(normalizeError(Error('mewo2'))).toBe('mewo2')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要 normalizeError({ message: '' }) 的 case


Expand Down
12 changes: 6 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { isObservableArray, IObservableArray } from 'mobx'
import { Validator, ValidatorReturned, ValidationError, ValidationErrorObject, ValidationResult } from './types'

export const inValidErrorObjectMsg = 'ValidationErrorObject message property cannot be empty'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid 是一个单词吧


// ValidationResult -> ValidationError
export function normalizeError(result: ValidationResult): ValidationError {
if (isErrorObject(result)) {
if (!result.message) {
throw new Error(inValidErrorObjectMsg)
}
return result.message
}

Expand All @@ -14,13 +19,8 @@ export function normalizeError(result: ValidationResult): ValidationError {
return result
}

export const inValidErrorObjectMsg = 'ValidationErrorObject message property cannot be empty'

export function isErrorObject(err: any): err is ValidationErrorObject {
if (err != null && typeof err === 'object' && 'message' in err) {
if (!err.message) {
throw new Error('ValidationErrorObject message property cannot be empty')
}
if (err != null && typeof err === 'object' && typeof err.message === 'string') {
return true
}
return false
Expand Down