Skip to content

Releases: volenslavchev/ts-runtime-checks

v0.6.3

Choose a tag to compare

@volenslavchev volenslavchev released this 10 Dec 18:46

Additions

  • Markers are now removed from typescript declaration files. For this to work, you must set the afterDeclarations plugin option to true. Important: If you set this option to true, then your normal .ts files won't get transformed, so you'll have to add this transformer twice, once with afterDeclarations set to true, and once set to false.

Bug fixes

  • Negative numbers get generated correctly (#68)
  • Support for special characters in property names (#63)

Changes

  • Support for typescript 5.7.x

v0.6.2

Choose a tag to compare

@volenslavchev volenslavchev released this 23 Sep 18:16

Additions

  • assertAll config option - Setting this option to true will add assertion code to ALL function parameters and as assertions. The assertion code will throw an error. You can use the NoCheck marker to override this behaviour. (#61)

Changes

  • Support for typescript 5.6.x

v0.6.1

Choose a tag to compare

@volenslavchev volenslavchev released this 19 Jul 14:32

Changes

  • Support for typescript 5.5.x (fixes #59)

v0.6.0

Choose a tag to compare

@volenslavchev volenslavchev released this 01 Jun 14:38

Additions

  • Transformations - You can now add transformation markers to types and use the transform utility function to perform the transformations:
    function timestampToDate(ts: number) : Date {
        return new Date(ts);
    }
    
    function transformAge(age: number) : number {
        return age + 1;
    }
    
    type User = {
        username: string,
        createdAt: Transform<typeof timestampToDate>,
        age: string & Transform<["+$self", typeof transformAge]>
    }
    
    const myUser: User = {
        username: "GoogleFeud",
        createdAt: 1716657364400,
        age: "123"
    }
    
    console.log(transform<User>(myUser))
    
    // Transpiles to:
    let result_1;
    result_1 = {};
    result_1.createdAt = timestampToDate(myUser.createdAt);
    result_1.age = transformAge(+myUser.age);
    result_1.username = myUser.username;
    console.log(result_1);
  • Undefined and Null types which can be used with conditional transformations and checks - null & Transform<...> is never, so these types need to exist.
  • PostCheck utility type which can be used alongside Transform to perform validation of the transformed values.

Changes

  • The playground now uses astro instead of next.js, and it's URL has been changed to googlefeud.github.io/ts-runtime-checks/playground. I'll be writing better and more interactive documentation over the next few weeks!
  • You'll no longer see stringified typescript types inside errors, they've been made a lot more readable.
  • Union type data now includes the possible variants in raw errors.
  • You can now use unions with check types: (implements #40)
    function validate1(
      param: Assert<Min<10> & Max<30> | string & (StartsWith<"a"> | MinLen<3>) | number[] & MinLen<3>>
    ) {
      return true;
    }
    
    // Transpiles to:
    function validate1(param) {
        if ((typeof param !== "string" || !param.startsWith("a") && param.length < 3) && (typeof param !== "number" || param < 10 || param > 30))
            if (!Array.isArray(param) || param.length < 3)
                throw new Error("Expected param to be number, to be greater than 10 & to be less than 30 | string, to start with \"a\" or to have a length greater than 3 | array<number>, to have a length greater than 3");
            else {
                for (let i_1 = 0; i_1 < param.length; i_1++) {
                    if (typeof param[i_1] !== "number")
                        throw new Error("Expected param[" + i_1 + "] to be a number");
                }
            }
        return true;
    }

Breaking changes

  • Removed the Or utility type - use the | operator.
  • Changed the Eq utility type to be more flexible, its parameters have been reversed, and the type is now optional.

v0.5.1

Choose a tag to compare

@volenslavchev volenslavchev released this 24 Mar 13:23

Bug fixes

Changes

  • Support typescript 5.4.x
  • Tiny performance optimizations
    • Mostly tweaking of weight values
    • typeof value === 'boolean' => value === true || value === false

v0.5.0

Choose a tag to compare

@volenslavchev volenslavchev released this 02 Mar 10:54

Additions

  • Check marker now accepts functions as well as string expressions:

    function complexValidation(value: unknown) {
        // Do validation...
    }
    
    type ComplexValidator =  Check<typeof complexValidation, "to pass complex validation">;
    
    function validate(user: Assert<ComplexValidator>) {
        // Your code...
    }
    
    validate('1234' as ComplexValidator);

    Transpiles to:

    function complexValidation(value) {}
    
    function validate(user) {
        if (!complexValidation(user))
            throw new Error("Expected user to pass complex validation.");
    }
    validate('1234');
  • The transformer now handles importing of values used inside markers. This means that the transformer is automatically going to import error classes, as well as functions used inside Checks. However, it's unable to import default exports, so make sure the stuff you pass to markers isn't exported as default.

Bug fixes

  • Mostly fixes #47 (see above for more info)

Changes

  • Min, Max, MinLen and MaxLen are now inclusive (#51)

v0.4.2

Choose a tag to compare

@volenslavchev volenslavchev released this 23 Dec 08:53

Bug fixes

Changes

  • Better code generation for index signatures, including support for Checks inside them. (#42).

v0.4.1

Choose a tag to compare

@volenslavchev volenslavchev released this 17 Oct 13:43
  • Support for typescript 5.2.x.

Changes

  • You can now combine multiple Checks without a base type.
  • Updated playground to display all logged messages.

Bug fixes

v0.4.0

Choose a tag to compare

@volenslavchev volenslavchev released this 14 Aug 15:16

Additions

  • A createMatch utility function that creates pattern-matching functions.

    // We want the match function to return a string and to accept a number
    const resolver = createMatch<string, number>([
        // Match arm which catches the values 0 or 1
        (_: 0 | 1) => "not many",
        // Match arm which catches any number less than 9
        (_: Max<9>) => "a few",
        // Match arm which catches any number that hasn't already been caught
        (_: number) => "lots"
    ]);
    
    // Transpiles to:
    const resolver = value_1 => {
        if (typeof value_1 === "number") {
            if (value_1 === 1 || value_1 === 0) return "not many";
            else if (value_1 < 9) return "a few";
            else return "lots";
        }
    };
  • Eq utility marker which compares a value to an expression, skipping validation of the value type.

Changes

  • Min, Max, Float, and Int utility markers now combine with the number type by default, so doing number & Min<3> for example is excessive.
  • Matches utility marker now combines with the string type by default.

Bug fixes

  • Unions that include the boolean type no longer generate excessive code.
  • Random comments no longer get added to output (#26)

v0.3.3

Choose a tag to compare

@volenslavchev volenslavchev released this 06 Aug 12:19

Additions

  • rawError option on check function.
  • Raw error data now includes ID and Value from Checks.

Bug fixes