胃肠紊乱吃什么药| 篱笆是什么| 腹腔肠系膜淋巴结是什么病| 甲鱼什么人不能吃| 中国的国宝是什么| 3月3号是什么星座| 处暑吃什么| 舌苔发青是什么原因| 十月份出生的是什么星座| 小腿酸胀痛是什么原因| 鼻甲肥大吃什么药最好| 小肚子胀疼是什么原因| 生气胸口疼是什么原因| 有什么树| o和ab型生的孩子是什么血型| 什么食物降血脂| 5月19号是什么星座| 软柿子是什么意思| 包粽子用什么米| 鼠的本命佛是什么佛| 无水奶油是什么| 如意是干什么用的| 副脾结节是什么意思| 笑靥如花什么意思| 早上口干舌燥是什么原因| 尼泊尔是什么人种| 张仲景的著作是什么| 什么东西能吃能喝又能坐| 脚踝浮肿是什么原因引起的| 衬衫搭配什么裤子好看| 子宫有问题有什么症状| 女人小肚子疼是什么原因| 补肾壮阳吃什么好| 经期吃什么| 5月29日什么星座| 飞亚达手表什么档次| 碳酸氢根偏低什么意思| 头热是什么原因| 半月板是什么| 交泰殿是干什么的| 一级军士长什么待遇| 杏花代表什么生肖| 为什么经常刷牙还牙黄| 敏感是什么意思| ipa啤酒是指什么| 风热感冒吃什么药好| 曲马多是什么药| 双马尾是什么意思| 什么蚊子咬人| 8.19是什么星座| 紫苏叶有什么功效| 招蚊子咬是什么原因| 双环征是什么意思| 聤耳是什么意思| 花甲不能和什么一起吃| 11月25是什么星座| 压抑是什么意思| 马齿苋长什么样子| 按摩手推是什么意思| dob是什么意思| 什么花好看| 为什么一进去就射了| sama是什么药| 高汤是什么意思| 鼻衄是什么意思| 牙齿发酸是什么病征兆| 七月份有什么节日吗| 私生子是什么意思| 喜悦之情溢于言表什么意思| 15度穿什么衣服合适| 灵魂摆渡是什么意思| 颈椎病引起的头晕吃什么药| 64岁属什么| 难产是什么意思| 蚂蚁怕什么| 腰椎间盘突出什么症状| 玮五行属什么| 乙肝携带者是什么意思| 羊水指数和羊水深度有什么区别| 私是什么意思| 舒筋健腰丸主治什么| 萎缩性胃炎能吃什么水果| 六扇门是什么意思| 不自觉摇头是什么病| 给你脸了是什么意思| 宫腔占位什么意思| 二十四节气分别是什么| 什么鸡没有翅膀| 法界是什么意思| 什么是老赖| 他喵的什么意思| 二五八万是什么意思| 子宫糜烂是什么症状| 相安无事什么意思| 穷凶极恶是什么生肖| 手抖是什么病的前兆| 什么是高纤维食物| 晚上9点到10点是什么时辰| 煮玉米放什么好吃| 红斑狼疮是什么病| 住院报销需要什么材料| 肺气泡吃什么药| 钙片吃多了有什么副作用| 水仙茶适合什么人喝| 吃鹅蛋对孕妇有什么好处| 抹茶粉是什么做的| 甲醛什么味| 1985年属什么生肖| 猕猴桃树长什么样| 误喝碘伏有什么伤害吗| 呆若木鸡的意思是什么| 什么是水肿| 二十不惑什么意思| 原发性肝ca什么意思| 眼干眼涩用什么眼药水| 低压高吃什么药效果好| 一个h是什么牌子| 猫爪草有什么功效| 绿色加蓝色是什么颜色| 补票是什么意思| alpha什么意思| 乌龟为什么不吃东西| 肌红蛋白偏低说明什么| 为什么不| 鱼吐泡泡是什么原因| 什么是火象星座| 吃枸杞有什么功效| 海豚吃什么食物| 淋漓不尽是什么意思| 锥切手术是什么意思| 五谷指的是什么| 身体缺钠会有什么症状| 团委书记是什么级别| 石斛有什么作用| 头晕是什么原因引起| 怀孕生化了有什么症状| 咖啡与什么食物相克| 1月20日什么星座| 姗字五行属什么| ect是什么检查| 吃什么卵泡长得快又好| 转氨酶偏高有什么症状| 米咖色是什么颜色| 喉炎吃什么药效果最好| 孕妇什么不能吃| 乳香是什么东西| 华盖星是什么意思| 神疲乏力是什么症状| 为什么手会掉皮| 水泻拉肚子是什么原因| 飞鸟集讲的是什么| 胃疼去医院挂什么科| 16 5是什么码| 补骨头吃什么最好| 五脏是什么| 本子什么意思| 什么叫肾阴虚和肾阳虚| 十五的月亮十六圆是什么意思| 参乌健脑胶囊适合什么人吃| hrv是什么病毒| 阴囊潮湿是什么原因造成的| 什么颜色加什么颜色等于白色| 一九七七年属什么生肖| 手术室为什么在三楼| 女人的排卵期一般是什么时候| 诸葛亮是什么生肖| 炎热的夏天风儿像什么| 愚昧是什么意思| 肝血虚吃什么食物调理| 嗓子沙哑是什么原因| 5p是什么意思| 尿里有泡沫是什么原因| 肾结水有什么危害| 舌头麻木是什么原因| 什么人容易心梗| 捡到狗狗代表什么预兆| 有福是什么意思| 胆碱酯酶高是什么意思| 1210是什么星座| 脑梗可以吃什么水果| 基数是什么意思| 用眼过度用什么眼药水| 家里为什么有小飞虫| 委屈是什么意思| 兔子的尾巴像什么| s和m是什么意思啊| 复查肺结节挂什么科| 意蕴什么意思| 无名指戴戒指代表什么| 疤痕增生是什么样子| 为什么叫韩国人棒子| 甲状腺是什么意思| 天蝎座属于什么象星座| 乙肝通过什么途径传染| 结巴是什么原因引起的| 脑病科是看什么病的| 膀胱尿潴留是什么意思| 巽是什么意思| 大学休学1年有什么影响| 小宇宙是什么意思| 约炮什么意思| 脚心发热吃什么药| fm是什么| 做梦梦到踩到屎是什么意思| 舅舅的女儿叫什么| 傻狍子什么意思| 指甲变黑是什么原因| 亢奋什么意思| 法界是什么意思| 孕妇口腔溃疡能用什么药| 吃什么减脂肪最快最有效的方法| 过期的酸奶有什么用途| 健脾胃吃什么| 什么叫知己| 瑶字五行属什么| 这个表情什么意思| 蟑螂喜欢什么环境| 扁桃体炎吃什么消炎药| 兔子的耳朵像什么| 尬是什么意思| 支原体感染吃什么药好| 为什么白带多| 舌头发涩是什么原因造成的| 田五行属性是什么| 验血能查出什么病| 什么叫滑精| 来加贝念什么| 母字是什么结构| 1223是什么星座| 铁为什么会生锈| 一月四号是什么星座| 多莉是什么鱼| 咳嗽痰中带血是什么原因| 血尿吃什么药见效快| 仙境是什么意思| 吃什么水果下火| 奕字五行属什么| 什么动物没有心脏| 妇科检查白细胞酯酶阳性是什么意思| 子宫内膜c型什么意思| 五月份是什么星座| 银杏是什么植物| 磷高吃什么药| 969368是什么电话| 下眼皮跳动是什么原因| 真狗是什么意思| 明前茶和明后茶有什么区别| 多巴胺是什么东西| 副主任科员是什么级别| 喉咙有异物感挂什么科| 上学是为了什么| 什么泡酒让性功能最强| 奎宁现在叫什么药| 大学校长什么级别| 女生为什么有喉结| 吃席是什么意思| 私生子是什么意思| 扭转乾坤是什么生肖| 60年属鼠是什么命| 什么叫快闪| 异父异母是什么意思| 上海有什么好玩的地方适合小孩子| 犯罪心理学属于什么专业| 口腔溃疡挂什么科| 百度
Skip to content

hf/toi

Repository files navigation

Toi

Build Status codecov

Toi is a validator for JavaScript values written in TypeScript.

It's heavily inspired by Joi, but actually uses different features from TypeScript's type system to infer the output type of a validation schema.

How to use?

It comes in two related packages:

  • @toi/toi which contains the most basic validators. You can see the source in packages/toi.
  • @toi/toix which contains extra and commonly used validators. You can see the source in packages/toix.

Unlike Joi, it takes a more flexible and monadic approach to building validation schemas.

Install

It's packaged under the organization scope @toi. Just do:

# for toi
yarn add @toi/toi
npm i --save @toi/toi

# for toix
yarn add @toi/toix
npm i --save @toi/toix

The major versions of these two packages will always be in sync. It's recommended you use the caret semver. Currently that is: @^1.0.0.

What's a validation schema?

A validation schema is just a function that has type information about what it's inputs must be and what the output must be. You invoke this function over the defined input, and you'll be sure to receive the defined output. Toi makes it easy to combine validation schemas to build rich output types.

A validator, i.e. a validation schema, has the type toi.Validator<Input, Output>. You can make your own validators easily by using the heper function toi.wrap.

Validators can be combined with the and function. In full honesty, it acts like the logical conjucation operator, but it's really more of a bind or map operator seen in monads. Effecitvely, it composes the validator function on which it's called and the provided validation function.

Each validator must obey these rules:

  • Must always accept null and undefined as values, unless really special.
  • Must always throw a ValidationError if the value is not expected.
  • Must throw any non-validation errors as early as possible.
  • Must return the correct value passed to them. This value may be transformed.

How it works?

Let's take a look at some common patterns and how they work with Toi.

Here's a validation schema for all objects of the form { num: number; str: string }.

import * as toi from "@toi/toi";

const isObject = toi
  .required() // make toi reject null or undefined
  .and(toi.obj.isplain()) // forces that the value is a plain JS object
  .and(
    toi.obj.keys({
      num: toi.required().and(toi.num.is()),
      str: toi.required().and(toi.str.is())
    })
  ); // makes sure that the object has props num, str that are never null

isObject({ num: 1, str: "1" }); // will pass
isObject(null); // will throw toi.ValidationError

You can use toi.required() or toi.optional() to enforce strict non-null rules. In TypeScript 2.8 there are conditional types, and these methods use them to infer whether the starting value is nullable, therefore propagate that type information down the chain.

It is sometimes possible to skip using these, if you already know what type you'll be validating. It's also possible to just not use them at all, in which case nullable type information will not be propagated down the chain. It doesn't mean tho, that the value is non-null!

All the validators!

Toi and ToiX are laid out in a specific way, to help you identify validators easily:

toi is the toplevel module. It contains the following submodules:

  • num includes validators that work with numbers
  • str includes validators that work with strings
  • bool includes validators that work with booleans
  • obj includes validators that work with objects
  • array includes validators that work with arrays
  • date includes validators that work with dates
  • any includes validators for any value
  • optional special validator for starting a nullable chain
  • required special validator for starting a non-nullable chain

Each submodule contains a method named is that does the most basic type check. For example: toi.num.is() validates that the value provided is a number, toi.str.is() validates that the value provided is a string, etc.

Inside each submodule you'll find different validators for different types of validations.

One of the goals for Toi is to have really readable code, and you should really take a look at it in order to find the validators you want. In fact, the website is just Toi!

Validation Errors

You can inspect each toi.ValidationError via the reasons property. If set, it will mimic the shape of the value that failed validation. So, if an object fails validation due to some of its properties failing validation, the reasons property will be an object with properties that map to other toi.ValidationErrors which caused the validation failure. Similarly it works for arrays.

Writing your own validators?

It's real easy. First, make sure you obey the rules for each validator. Otherwise, there are three methods that will make your life easier:

  1. toi.wrap("name-in-stack-trace", value => value) to convert a simple validation function into a full-blown toi.Validator.
  2. toi.allow(value => !value, "value is truthy") to create a simple validation function that obeys the rules, from a function that returns true if the validation passes, or false if it does not. You still have to use toi.wrap on top of this.
  3. toi.transform(value => value + 1) to create a simple validation function that transforms a value into a different value, and obeys the rules. You still have to use toi.wrap on top of this.

Contributing

Only the basic validators out of which you can build more complex validators should be placed in the toi package. Everything else should go into toix.

Test coverage must be 100% for both packages. If you don't like it, please make your own library. Toi was designed to be easy to interoperate with custom validators (unlike Joi).

Tests must test what the documentation says. Don't test JavaScript runtime methods, but do explicitly say that they're used.

License

Copyright ? 2018 Stojan Dimitrovski, some rights reserved.

Licensed under the MIT license. You can get a copy of it in LICENSE.

About

A TypeScript validation library capable of inferring types

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5

中国最长的河流是什么河 dv是什么牌子 嘴唇暗紫色是什么原因 凌晨属于什么时辰 2018年属什么生肖
s和m是什么意思 10.5号是什么星座 膀胱壁毛糙是什么原因 吃什么能补钙 秋葵有什么营养价值
牛鬼蛇神是什么意思 菩提子是什么树的种子 骨外科是看什么病的 05是什么生肖 喉咙肿瘤有什么症状
什么是中成药 破伤风什么情况需要打 脑供血不足吃什么食物 十二指肠球炎是什么意思 素鲍鱼是什么做的
为什么合欢树又叫鬼树bjhyzcsm.com 回执单是什么意思hcv8jop0ns5r.cn 什么事的英文hcv9jop0ns2r.cn 农历六月初十是什么日子hcv9jop4ns8r.cn 前门大街有什么好玩的hcv7jop5ns2r.cn
发烧呕吐是什么原因hcv8jop4ns0r.cn 孕妇梦见龙是什么征兆hcv8jop0ns4r.cn 加应子是什么水果hcv8jop7ns6r.cn 布五行属什么hcv8jop7ns4r.cn 什么可以解酒hcv8jop6ns9r.cn
曲安奈德针治疗什么hcv8jop8ns4r.cn 中秋节送什么hcv9jop0ns1r.cn 铁锈色痰见于什么病hcv8jop6ns1r.cn 诛是什么意思hcv9jop6ns2r.cn 大腿青筋明显是什么原因hcv9jop2ns5r.cn
胃疼需要做什么检查hcv8jop9ns2r.cn 一个月的小猫吃什么bjcbxg.com 什么是末法时代hcv8jop4ns1r.cn 凉拖鞋什么材质的好hcv8jop0ns4r.cn 梦见蛇追我是什么预兆hcv8jop4ns7r.cn
百度