I’m very excited to introduce the Guardian library as part of the Metro UI project. The library is designed for data validation according to a pre-built scheme. Although the library is positioned as part of Metro UI, it can be used separately from Metro UI.
When used with Metro UI, Guardian is available in the globalThis.G
namespace.
Usage Example
Checkin one guardian
const schema = G.email()
const data = G.parse(schema, "vasya@pupkin.com")
Checking with a lot of guardians
// pipe - check left to right
const schema = G.pipe(required(), email())
const data = G.parse(schema, "vasya@pupkin.com")
// compose - check right to left
const schema = G.compose(required(), email())
const data = G.parse(schema, "vasya@pupkin.com")
Checking object data
const schema = G.object({
name: string(),
email: email(),
cards: object({
visa: visa(),
mastercard: mastercard()
})
})
const data = G.parse(schema, {...})
Parsers
- -
G.parse()
- Parsing with throw exception - -
G.safeParse()
- Parsing without exception
Pipes
-
G.pipe()
- pipe with check left to right -
G.compose()
- pipe with check right to left
Available Guardians
- array
- base64
- between
- bigint
- boolean
- bytes
- creditCard (visa, mastercard, discover, jbc, diners, unionPay, americanExpress)
- date
- digits
- domain
- empty
- endsWith
- finite
- float
- func
- hexColor
- imei
- instance
- integer
- ip (ipv4, ipv6)
- length
- maxValue
- minValue
- notNull
- notNumber
- number
- pattern
- promise
- required
- safeInteger
- startsWith
- string
- symbol
- unknown
- url