-
Notifications
You must be signed in to change notification settings - Fork 9
[RMBWEB-2780] Support for structured validation results #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
83cd99a
8d68591
f466115
db9f109
227f425
d84c44a
90e4499
78f35e5
b2f88ba
e5afe2a
3e45f64
511d66f
b8fb666
9065ddf
5d56725
2c76ca8
a8cb353
031f8a1
ba4b45e
dbba8f5
a5ec69d
82e505b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import * as v2 from 'formstate-x-v2' | |
| import { BaseState } from '../state' | ||
| import * as v3 from '..' | ||
| import Disposable from '../disposable' | ||
| import { isPromiseLike, normalizeError } from '../utils' | ||
|
|
||
| interface IV3StateFromV2<T extends v2.ComposibleValidatable<unknown, V>, V> extends v3.IState<V> { | ||
| /** The original ([email protected]) state */ | ||
|
|
@@ -27,6 +28,7 @@ class Upgrader<T extends v2.ComposibleValidatable<unknown, V>, V> extends BaseSt | |
| @computed get ownError() { | ||
| 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() { | ||
|
|
@@ -47,7 +49,7 @@ class Upgrader<T extends v2.ComposibleValidatable<unknown, V>, V> extends BaseSt | |
| isV2FieldState(this.stateV2) | ||
| || isV2FormState(this.stateV2) | ||
| ) { | ||
| this.stateV2.validators(...validators) | ||
| this.stateV2.validators(...portV2Validators(...validators)) | ||
| return this | ||
| } | ||
| throwNotSupported() | ||
|
|
@@ -64,7 +66,23 @@ class Upgrader<T extends v2.ComposibleValidatable<unknown, V>, V> extends BaseSt | |
| } | ||
| } | ||
|
|
||
| /** Convets [email protected] state to [email protected] state */ | ||
| function portV2Validators<V>(...validators: Array<v3.Validator<V>>): Array<v2.Validator<V>> { | ||
| const normalizeRet = (v: any) => ( | ||
| normalizeError(v) | ||
| ) | ||
| return validators.map(validator => { | ||
| return (value: V) => { | ||
| const returned = validator(value) | ||
| if (isPromiseLike(returned)) { | ||
| return returned.then(normalizeRet) | ||
| } else { | ||
| return normalizeRet(returned) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /** Converts [email protected] state to [email protected] state */ | ||
| export function fromV2<T extends v2.ComposibleValidatable<unknown, unknown>>(stateV2: T): IV3StateFromV2<T, T['value']> { | ||
| return new Upgrader(stateV2) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 } from './utils' | ||
| import { debounce, isPassed, normalizeError } from './utils' | ||
|
|
||
| const defaultDelay = 200 // ms | ||
|
|
||
| /** Infomation synced from original state */ | ||
| type OriginalInfo<V> = Pick<IState<V>, 'activated' | 'touched' | 'error' | 'ownError'> | ||
| type OriginalInfo<V> = Pick<IState<V>, 'activated' | 'touched' | 'error' | 'ownError' | 'hasError'> | ||
|
|
||
| /** | ||
| * The state for debounce purpose. | ||
|
|
@@ -42,7 +42,8 @@ export class DebouncedState<S extends IState<V>, V = ValueOf<S>> extends Validat | |
| activated: this.$.activated, | ||
| touched: this.$.touched, | ||
| error: this.$.error, | ||
| ownError: this.$.ownError | ||
| ownError: this.$.ownError, | ||
| hasError: this.$.hasError | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -52,7 +53,7 @@ export class DebouncedState<S extends IState<V>, V = ValueOf<S>> extends Validat | |
|
|
||
| @override override get ownError() { | ||
|
||
| if (this.disabled) return undefined | ||
| if (this._error) return this._error | ||
| if (this.rawError) return normalizeError(this.rawError) | ||
| return this.original.ownError | ||
| } | ||
|
|
||
|
|
@@ -62,6 +63,11 @@ 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 ( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: 后边有时间的时候,最好是在文档 Guide/Advanced 里补一下对应的 case(可以不在这个 PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK..