Package Exports
- @srhenry/type-utils
- @srhenry/type-utils/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@srhenry/type-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Type Utils
Type utilities module for Typescript and also Javascript. It can secure your application from invalid data being pushed inside and breaking things as it can shape and model your data to prevent invalid data. Check out the documentation for further details.
Table of Contents
Installing
npm install @srhenry/type-utils --saveor from git repository:
git clone git@gitlab.com:SrHenry/type-utils.git
cd storage-manager
npm run buildDocs
Schema types
Schema.stringIt represents a string to typescript's type infers and runtime validation
import { Schema } from "@srhenry/type-utils" const isString = Schema.string()
Schema.numberIt represents a number to typescript's type infers and runtime validation
import { Schema } from "@srhenry/type-utils" const isNumber = Schema.number()
Schema.booleanIt represents a number to typescript's type infers and runtime validation
import { Schema } from "@srhenry/type-utils" const isBoolean = Schema.boolean()
Schema.objectIt represents a well defined object to typescript's type infers and runtime validation, which its properties are also described using the Schema helpers
import { Schema } from "@srhenry/type-utils" const isMyObject = Schema.object({ foo: Schema.string(), bar: Schema.number(), })
Schema.arrayIt represents an array to typescript's type infers and runtime validation, which its items can also be described using the Schema helpers
import { Schema } from "@srhenry/type-utils" const isArray = Schema.array() // array of anything const isMyArray = Schema.array(Schema.string()) // array of strings
Schema.symbolIt represents a symbol to typescript's type infers and runtime validation
import { Schema } from "@srhenry/type-utils" const isSymbol = Schema.symbol()
Schema.asEnumIt represents a closed switch values to typescript's type infers and runtime validation. It ensures your value is one of the values given in params.
import { Schema } from "@srhenry/type-utils" enum Status { ready: 1, running: 2, stopped: 3, } const validStatus = [...Object.keys(Status)] as (keyof typeof Status)[] const isStatus = Schema.asEnum(validStatus)
Schema.asNullIt represents a null literal to typescript's type infers and runtime validation.
import { Schema } from "@srhenry/type-utils" const isNull = Schema.asNull()
Schema.primitiveIt represents a primitive values (such as string, number, boolean, symbol, null, undefined) to typescript's type infers and runtime validation.
import { Schema } from "@srhenry/type-utils" const isSymbol = Schema.primitive()
Schema.anyIt represents a 'any' value to typescript's type infers and runtime validation. It does nothing to validate a narrowed type but can be useful to improve readability in more complex schemas.
import { Schema } from "@srhenry/type-utils" const isAny = Schema.any() const objectHasFoo = Schema.object({ foo: isAny }) //it checks if is object and if has `foo` property but doesn't care checking its type
Schema.optionalSchema.optional.*It represents a optional value to typescript's type infers and runtime validation. It returns recursive structure of schema helpers to narrow validation.
import { Schema } from "@srhenry/type-utils" const maybeString = Schema.optional().string() const objectMaybeHasFoo = Schema.object({ foo: maybeString }) //it checks if is object and if has `foo`. if it has `foo` then check if it is string, if it hasn't then pass anyway as it is optional property
Schema helpers
Schema.andIt creates an intersection between two or more schemas.
import { Schema } from "@srhenry/type-utils" const hasFoo = Schema.object({ foo: Schema.string() }) const hasBar = Schema.object({ bar: Schema.string() }) const isSomething = Schema.and(hasFoo, hasBar)
Schema.orIt creates an union between two or more schemas.
import { Schema } from "@srhenry/type-utils" const isString = Schema.string() const isBool = Schema.boolean() const isSomething = Schema.or(isString, isBool)
Schema.andIt creates an union between two or more schemas.
import { Schema } from "@srhenry/type-utils" const hasFoo = Schema.object({ foo: Schema.string() }) const hasBar = Schema.object({ bar: Schema.string() }) const isSomething = Schema.and(hasFoo, hasBar)
Schema.useSchemaIt wraps a schema (just to improve readability).
import { Schema } from "@srhenry/type-utils" const hasFoo = Schema.object({ foo: Schema.string() }) const isFooArray = Schema.array(Schema.useSchema(hasFoo))
Validation rules
Number.nonZeroIt constraints a number to be different from 0.
import { Schema, Rules } from "@srhenry/type-utils" const isNonZeroNumber = Schema.number([Rules.Number.nonZero()])
Number.maxIt constraints a number to be lesser than a given number.
import { Schema, Rules } from "@srhenry/type-utils" const isNonZeroNumber = Schema.number([Rules.Number.max(255)])
Number.minIt constraints a number to be greater than a given number.
import { Schema, Rules } from "@srhenry/type-utils" const isNonZeroNumber = Schema.number([Rules.Number.min(1)])
Array.maxIt constraints an array's size to be lesser than a given number.
import { Schema, Rules } from "@srhenry/type-utils" const isArray = Schema.array([Rules.Array.max(25)], Schema.any())
Array.minIt constraints an array's size to be greater than a given number.
import { Schema, Rules } from "@srhenry/type-utils" const isArray = Schema.array([Rules.Array.min(1)], Schema.any)
Array.uniqueIt constraints an array to contain only distinct values, failling if a duplicate is found.
import { Schema, Rules } from "@srhenry/type-utils" const isArray = Schema.array([Rules.Array.unique()], Schema.string())
String.maxIt constraints a string's size to be lesser than a given number.
import { Schema, Rules } from "@srhenry/type-utils" const isString = Schema.string([Rules.String.max(60)])
String.minIt constraints a string's size to be greater than a given number.
import { Schema, Rules } from "@srhenry/type-utils" const isString = Schema.string([Rules.String.min(60)])
String.regexIt constraints a string to match a given pattern (regular expression).
import { Schema, Rules } from "@srhenry/type-utils" const isNumericString = Schema.string([Rules.String.regex(/[0-9]+/)])
String.nonEmptyIt constraints a string's size to be greater than 0.
import { Schema, Rules } from "@srhenry/type-utils" const isString = Schema.string([Rules.String.nonEmpty()])
Available validations
isIt checks a given value against a given schema or validator and return true if schema matches the value, otherwise return false.
import { Schema, is } from "@srhenry/type-utils" //... if ( is( value, Schema.string() ) ) { // value is string } else { // value is not a string }
ensureInterfaceIt checks a given value against a given schema or validator and returns the checked value with schema inferred type if schema matches the value or throws an error if schema didn't match the value. Pretty clean to use with destructuring pattern.
import { Schema, ensureInterface } from "@srhenry/type-utils" //... const { foo, bar } = ensureInterface( value, Schema.object({ foo: Schema.number(), bar: Schema.string() }) ) //throws error if validation fails! console.log("foo", foo) console.log("bar", bar)
NOTE: You can use schema directly to validate a value.
Ex.:
import { Schema } from "@srhenry/type-utils" const hasFoo = Schema.object({ foo: Schema.number() }) ) //... if ( hasFoo(obj) ) { // obj is object and contains a string property named `foo` } else { // obj don't have a `foo` property of type string }