Skip to content

[FEATURE] Probability checks #73

@shipurjan

Description

@shipurjan

Is your feature request related to a problem? Please describe.
Add a configuration option to only check x% of function calls, because I don't need to check the schema every time, just enough times to get a notification if something goes wrong.

Describe the solution you'd like

"compilerOptions": {
//... other options
"plugins": [
        {
            "transform": "ts-runtime-checks",
            "jsonSchema": {
                "dist": "./schemas"
            },
            "probability": 0.01
        }
    ]
}
// Special `Assert` type get detected and generates validation code
function greet(name: Assert<string>, age: Assert<number>): string {
    return `Hello ${name}, you are ${age} years old!`;
}

// Transpiles to:
function greet(name, age) {
    if (Math.random() < 0.01) {
      if (typeof name !== "string") throw new Error("Expected name to be a string");
      if (typeof age !== "number") throw new Error("Expected age to be a number");
    }
    return `Hello ${name}, you are ${age} years old!`;
}

Another solution would be to create another type like ProbableAssert which does this.

It's probably not a standard for validation libraries to have this, but I thought it's a cool feature

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions