-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement struct validation result * fix typo * restruct normalize raw error * update state comment * fix typo * remove rawError from ValidateResultWithError * update test case * fix typo * update check error condition * remove debug info * add test case * fix throw error test case * restruct error hierarchy * restruct docs and misc * fix test case * update comments * rename _error * fix override state props * abstract error * fix infinity error * fix typo * fix typo
- Loading branch information
Showing
14 changed files
with
389 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | ||
|
@@ -24,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() { | ||
@computed get rawError() { | ||
return getV3OwnError(this.stateV2) | ||
} | ||
@computed get error() { return this.stateV2.error } | ||
|
@@ -47,7 +48,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 +65,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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.