Typeteca
Typeteca is a library of brand new Types on top of built-in String, Number, etc. Each new Type has it's own validation mechanism, so that you can use any data essence like it is native JS data type 💥🎉🌟
It is tiny and zero-dependencies package.
Motivation
One abstract Type library for different validation and schema libraries. Share the same validation rules across different integration endpoints.
Usage
Installing package:
npm i typeteca
Importing Types and standard usage:
const Email = // orconst Email = const email = 'username@example.com' console // [String: 'username@example.com']console // username@example.com
What if value has invalid format?
try const email = 'usernameexample.com' catch error console // ValidationError console // Incorrect value format: expected valid email format
But passing empty value does not throw any error:
const email = console // [String: '']
Each of Types inherits one of built-in JavaScript ones:
const email = 'username@example.com' console //true
You can create custom Types on top of Typeteca:
const CustomString = { if !/^.{6,30}$/ throw 'Incorrect value format: expected valid password format' }
CustomString util class extends String with additional validation layer:
try const password = 69 catch error console // ValidationError console // Incorrect value type - Password: expected valid String type
You can use ValidationError to keep unified error handling flow
const Uri ValidationError = const uri = 'https://example.com' if uri throw 'Uri might reffer to http protocol only'
Types & APIs
See the detailed API Reference.
Use Cases
All about using Typeteca during modeling your api endpoints in Express or Hapi, extending Mongoose Schemas with brand new Types, or other useful cases - see the detailed Use Cases Reference.
Integrations and Plugins - Quick Reference
typeteca-mongoose
- helps easily integrate Typeteca Types into Mongoose Schema registry- to be continued...
License
This project is licensed under the MIT License - see the LICENSE file for details.