Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/adapter/v2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { action, computed, makeObservable } from 'mobx'
import { override, action, computed, makeObservable } from 'mobx'
import * as v2 from 'formstate-x-v2'
import { BaseState } from '../state'
import * as v3 from '..'
Expand All @@ -25,7 +25,7 @@ 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() {
@override override get ownError() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这边之所以需要 override,恰恰反映是逻辑上实现反了?应该是基于 rawError 得到 ownError,这里做成了基于 ownError 得到 rawError

return getV3OwnError(this.stateV2)
}
@computed get rawError() { return this.ownError }
Expand Down
20 changes: 9 additions & 11 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { action, autorun, computed, makeObservable, observable, when } from 'mobx'
import { ValidationResult, ValidationError, IState, Validation, ValidateResult, ValidateStatus, Validator } from './types'
import { ValidationResult, IState, Validation, ValidateResult, ValidateStatus, Validator } from './types'
import Disposable from './disposable'
import { applyValidators, isPromiseLike, isPassed, 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 感觉怪怪的


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

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

@computed get hasOwnError() {
return !!this.ownError
Expand Down Expand Up @@ -56,14 +58,10 @@ export abstract class ValidatableState<V> extends BaseState implements IState<V>
/**
* The original return value of validation.
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个注释也改一下?

Suggested change
* The original return value of validation.
* The original validation result.

*/
@observable private _error: ValidationResult
@observable private 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 +72,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 +165,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
6 changes: 1 addition & 5 deletions src/transformedState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, override } from 'mobx'
import { override, computed } from 'mobx'
import { BaseState } from './state'
import { IState, Validator, ValueOf } from './types'

Expand All @@ -23,10 +23,6 @@ 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
}
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
10 changes: 5 additions & 5 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里是不是最好再加个 typeof err.message === 'string'

Copy link
Collaborator

Choose a reason for hiding this comment

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

P.S. 如果加了 typeof err.message === 'string',好像 'message' in err 也可以省了

if (!err.message) {
throw new Error('ValidationErrorObject message property cannot be empty')
}
return true
}
return false
Expand Down