Coder Social home page Coder Social logo

hf / toi Goto Github PK

View Code? Open in Web Editor NEW
27.0 1.0 8.0 411 KB

A TypeScript validation library capable of inferring types

License: MIT License

TypeScript 97.39% Shell 1.94% JavaScript 0.66%
typescript validation validator validation-schema joi joi-validation dynamodb

toi's Issues

Validating `Record` Types?

Is there a way to validate to Record types in TOI? Specifically, how would I define validator such that:

const foo:Record<string,unknown> = validator(bar);

instanceof fails for ValidationError when there are multiple toi versions in one monorepo

The instanceof operator doesn't always work when there are multiple versions of toi in one monorepo, for example. Different require statements result in multiple ValidationError classes and the check that is performed with instanceof is no longer reliable.

One approach on how can this be fixed is by introducing a boolen flag, like isToi, to the ValidationError class that will identify the error. The Joi library has the same approach.

Misleading validator name for num.isNan

Although there is a clear JSDoc explaining what this validator does, the name of the validator itself is misleading. I expect that it would check that a value is NaN, but it actually checks if a value is of type number. This means that all valid numbers (excluding NaN) will pass this check. I thing that this validator should be named num.isNumberType and there should be another validator which checks if the value is really NaN and it could be named num.isNaN. Will submit a PR.

Missing property in toi.obj.keys infers the type from missing

const validator = toi
      .required()
      .and(toi.obj.is())
      .and(
        toi.obj.keys(
          {
            a: toi
              .required()
              .and(toi.str.is())
              .and(toix.str.guid()),
            b: toi.required().and(toi.num.is()),
            c: toi.required().and(toi.num.is()),
            d: toi
              .optional()
              .and(toi.str.is())
              .and(toi.str.regex(/^([0-9]|[a-z])+([0-9a-z]+)$/i)),
          },
          { missing: ['d'] },
        ),
      );

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.