Awesome
<div align="center"> <h1>📐</br>rulr</h1> <p>Validation and <a href="./docs/unitConversionErrorProtection.md">unit conversion errors</a> in TypeScript at compile-time.</p> <a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-d9207b.svg" alt="License: MIT"></a> <a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%F0%9F%93%A6%F0%9F%9A%80%20-semantic%20release-d9207b.svg" alt="Uses Semantic Release to correctly bump versions especially for breaking changes"></a> <a href="https://renovatebot.com/"><img src="https://img.shields.io/badge/%F0%9F%94%84%F0%9F%A4%96%20-renovate%20bot-d9207b.svg" alt="Uses Renovate to keep dependencies updated"></a> <a href="https://codecov.io/gh/ryasmi/rulr"><img alt="Master branch coverage percentage from Codecov" src="https://codecov.io/gh/ryasmi/rulr/branch/master/graph/badge.svg" /></a> <a href="https://bundlephobia.com/result?p=rulr"><img alt="Package size from BundlePhobia" src="https://img.shields.io/bundlephobia/minzip/rulr.svg" /></a> <div> </div> </div>// Install it with `npm i rulr`
import * as rulr from 'rulr'
// Compile-time error.
const positiveNumber1: rulr.PositiveNumber = -1
// Run-time error.
const positiveNumber2 = rulr.positiveNumber(-1)
// Convenient rules and guards like `object`.
const example = rulr.object({
required: {
price: rulr.positiveNumber,
},
})
// Turn rules into types to avoid duplicating information.
type Example = rulr.Static<typeof example>
// Turn rules into guards to avoid duplicating code.
const isExample = rulr.guard(example)
// Use rules and/or guards to guarantee your data is valid.
const myExample: Example = example({ price: 12.34 })
if (isExample(myExample)) {
console.log(myExample.price)
}
Getting Started ✨
To save you some time, Rulr comes with the following rules.
- allowNull
- allowUndefined
- any
- array
- bigint
- boolean
- constant
- date
- dictionary
- enum
- literal
- number
- object
- string
- symbol
- tuple
- union
- unknown
Sized Strings
Since it's quite common to want to restrict the size of strings to avoid UI overflow bugs and DB storage errors, Rulr comes with some convenient rules for doing just that.
- nonEmptyString
- tinyText (0 - 255 characters)
- text (0 - 65,535 characters)
- mediumText (0 - 16,777,215 characters)
Constraining Strings
Rulr also comes with a growing list of convenient rules for constraining strings that are mostly built on Chris O'Hara's extensive and much loved validator package. As with the rules for sized strings above, this can help prevent UI overflow bugs, DB storage errors, and processing errors.
- iri
- iso8601Duration
- iso8601FullDate
- iso8601Timestamp
- locale
- mailto
- mimeType
- mongoId
- scormInteractionType
- semanticVersion
- sha1
- url
- uuidv4
Constraining Non-Strings
In addition to the constrained strings, Rulr also comes with a few convenient rules to help you quickly validate non-string values.
Sanitization Rules
Finally, Rulr is starting to provide rules that sanitize inputs from HTTP headers and URL params.
- sanitizeBooleanFromString
- sanitizeJsonFromString
- sanitizeNumberFromString
- sanitizeBasicAuthFromString
- sanitizeJWTBearerAuthFromString
Frequently Awesome Questions 🤘
- How does Rulr protect against unit conversion errors?
- How do I create my own rules?
- Can I add new rules to this package?
- Why not use classes?
- Why are unique symbols needed for each type?
- How are recursive rules defined?
- Can Rulr be used with React Hooks and PropTypes?
- Can Rulr be used with Express?
- Why are you using tabs!?!
- Why is the bundle size quite large?
- How does the performance compare to other validation packages?
- How often are breaking changes/major version bumps released?
Background
Rulr was started in 2016 and first publicised in 2020. It continues to be maintained to save us time writing validation logic and correcting data by returning as many validation errors as possible in one function call.
Rulr has been influenced by Tom Crockett in RunTypes and more recently Colin McDonnell in Zod. It's hoped that if nothing else, publicising Rulr will influence existing and future validation packages for the better.