-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom useParams and useSearchParams
- Loading branch information
Showing
3 changed files
with
143 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,80 @@ | ||
import { | ||
type InferType, | ||
type ValidateOptions, | ||
ValidationError, | ||
type AnyObject, | ||
type DefaultFromShape, | ||
type InferType, | ||
type MakePartial, | ||
type ObjectSchema, | ||
type ObjectShape, | ||
type Schema, | ||
type TypeFromShape, | ||
type ValidateOptions, | ||
} from "yup" | ||
|
||
export function tryValidateSync<Schema extends ObjectSchema<any>>( | ||
export type _<T> = T extends {} | ||
? { | ||
[k in keyof T]: T[k] | ||
} | ||
: T | ||
|
||
export type MakeKeysOptional<T> = T extends AnyObject ? _<MakePartial<T>> : T | ||
|
||
export type ObjectFromShape<Shape extends ObjectShape> = MakeKeysOptional< | ||
_<TypeFromShape<Shape, AnyObject>> | ||
> | ||
|
||
export type ObjectSchemaFromShape<Shape extends ObjectShape> = ObjectSchema< | ||
_<TypeFromShape<Shape, AnyObject>>, | ||
AnyObject, | ||
_<DefaultFromShape<Shape>>, | ||
"" | ||
> | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Try Validate Sync | ||
// ----------------------------------------------------------------------------- | ||
|
||
export type TryValidateSyncOnErrorRT<S extends Schema> = InferType<S> | void | ||
|
||
export type TryValidateSyncRT< | ||
S extends Schema, | ||
OnErrorRT extends TryValidateSyncOnErrorRT<S>, | ||
> = OnErrorRT extends InferType<S> ? InferType<S> : InferType<S> | undefined | ||
|
||
export type TryValidateSyncOptions< | ||
S extends Schema, | ||
OnErrorRT extends TryValidateSyncOnErrorRT<S>, | ||
> = ValidateOptions & { | ||
onError?: (error: ValidationError) => OnErrorRT | ||
} | ||
|
||
export function tryValidateSync<S extends Schema>( | ||
value: any, | ||
schema: Schema, | ||
kwArgs?: { | ||
options?: ValidateOptions | ||
}, | ||
): InferType<Schema> | undefined | ||
schema: S, | ||
options?: ValidateOptions, | ||
): InferType<S> | undefined | ||
|
||
export function tryValidateSync< | ||
Schema extends ObjectSchema<any>, | ||
OnErrorRT extends InferType<Schema> | void, | ||
S extends Schema, | ||
OnErrorRT extends TryValidateSyncOnErrorRT<S>, | ||
>( | ||
value: any, | ||
schema: Schema, | ||
kwArgs?: { | ||
options?: ValidateOptions | ||
schema: S, | ||
options?: ValidateOptions & { | ||
onError: (error: ValidationError) => OnErrorRT | ||
}, | ||
): OnErrorRT extends InferType<Schema> | ||
? InferType<Schema> | ||
: InferType<Schema> | undefined | ||
): TryValidateSyncRT<S, OnErrorRT> | ||
|
||
export function tryValidateSync< | ||
Schema extends ObjectSchema<any>, | ||
OnErrorRT extends InferType<Schema> | void, | ||
>( | ||
value: any, | ||
schema: Schema, | ||
kwArgs?: { | ||
options?: ValidateOptions | ||
onError?: (error: ValidationError) => OnErrorRT | ||
}, | ||
): InferType<Schema> | undefined { | ||
S extends Schema, | ||
OnErrorRT extends TryValidateSyncOnErrorRT<S>, | ||
>(value: any, schema: S, options?: TryValidateSyncOptions<S, OnErrorRT>) { | ||
const { onError, ...validateOptions } = options || {} | ||
|
||
try { | ||
return schema.validateSync(value, kwArgs?.options) | ||
return schema.validateSync(value, validateOptions) | ||
} catch (error) { | ||
if (!(error instanceof ValidationError)) { | ||
throw error | ||
} else if (kwArgs?.onError !== undefined) { | ||
return kwArgs.onError(error) as InferType<Schema> | undefined | ||
} | ||
if (!(error instanceof ValidationError)) throw error | ||
else if (onError) return onError(error) | ||
} | ||
|
||
return undefined | ||
} |