Skip to content

Commit

Permalink
Merge pull request #185 from samchon/v3.2
Browse files Browse the repository at this point in the history
Fix #184, validators support unknown type
  • Loading branch information
samchon authored Sep 12, 2022
2 parents cc898ab + 40469b2 commit 4427bd3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "3.2.2",
"version": "3.2.3",
"description": "Runtime type checkers and 5x faster JSON.stringify() function",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
63 changes: 62 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ export * from "./TypeGuardError";
*/
export function assertType<T>(input: T): T;

/**
* Asserts a value type in the runtime.
*
* Asserts a parametric value type and throws a {@link TypeGuardError} with detailed
* reason, if the parametric value is not following the type `T`. Otherwise, the
* value is following the type `T`, just input parameter would be returned.
*
* If what you want is not asserting but just knowing whether the parametric value is
* following the type `T` or not, you can choose the {@link is} function instead.
* Otherwise you want to know all the errors, {@link validate} is the way to go.
*
* @template T Type of the input value
* @param input A value to be asserted
* @returns Parametric input value casted as `T`
* @throws A {@link TypeGuardError} instance with detailed reason
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function assertType<T>(input: unknown): T;

/**
* @internal
*/
Expand Down Expand Up @@ -92,6 +112,27 @@ export namespace assertType {
*/
export function is<T>(input: T): input is T;

/**
* Tests a value type in the runtime.
*
* Tests a parametric value type and returns whether it's following the type `T` or not.
* If the parametric value is matched with the type `T`, `true` value would be returned.
* Otherwise, the parametric value is not following the type `T`, `false` value would be
* returned.
*
* If what you want is not just knowing whether the parametric value is following the
* type `T` or not, but throwing an exception with detailed reason, you can choose
* {@link is} function instead. Also, if you want to know all the errors with detailed
* reasons, {@link validate} function would be useful.
*
* @template T Type of the input value
* @param input A value to be tested
* @returns Whether the parametric value is following the type `T` or not
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function is<T>(input: unknown): input is T;

/**
* @internal
*/
Expand Down Expand Up @@ -126,10 +167,30 @@ export namespace is {
*
* @template Type of the input value
* @param input A value to be validated
* @returns
* @returns Validation result
*/
export function validate<T>(input: T): IValidation;

/**
* Validate a value type in the runtime.
*
* Validates a parametric value type and archives all the type errors into an
* {@link IValidation.errors} array, if the parametric value is not following the
* type `T`. Of course, if the parametric value is following the type `T`, the
* {@link IValidation.errors} array would be empty and {@link IValidation.success}
* would have the `true` value.
*
* If what you want is not finding all the error, but asserting the parametric value
* type with exception throwing, you can choose {@link assertType} function instead.
* Otherwise, you just want to know whether the parametric value is matched with the
* type `T`, {@link is} function is the way to go.
*
* @template Type of the input value
* @param input A value to be validated
* @returns Validation result
*/
export function validate<T>(input: unknown): IValidation;

/**
* @internal
*/
Expand Down
6 changes: 6 additions & 0 deletions test/issues/184.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import TSON from "../../src/index";

const value: string | number = "something" as any;
if (TSON.is<string>(value)) {
TSON.is(value);
}

0 comments on commit 4427bd3

Please sign in to comment.