hf / toi Goto Github PK
View Code? Open in Web Editor NEWA TypeScript validation library capable of inferring types
License: MIT License
A TypeScript validation library capable of inferring types
License: MIT License
None
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);
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.
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.
something like:
toi
.optional({default:20})
.and(toi.num.is())
or just:
toi
.optional(20)
.and(toi.num.is())
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'] },
),
);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.