typeteca
v1.0.2
Published
Brand new Types for modern JS application
Downloads
5
Maintainers
Readme
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 :boom:🎉:star2:
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} = require('typeteca')
// or
const Email = require('typeteca/types/email')
const email = new Email('[email protected]')
console.log(email) // [String: '[email protected]']
console.log(email.valueOf()) // [email protected]
What if value has invalid format?
try {
const email = new Email('usernameexample.com')
} catch (error) {
console.log(error.name) // ValidationError
console.log(error.message) // Incorrect value format: expected valid email format
}
But passing empty value does not throw any error:
const email = new Email()
console.log(email) // [String: '']
Each of Types inherits one of built-in JavaScript ones:
const email = new Email('[email protected]')
console.log(email instanceof String) //true
You can create custom Types on top of Typeteca:
const {CustomString} = require('typeteca')
class Password extends CustomString {
validate(value){
if (!/^.{6,30}$/.test(value))
throw 'Incorrect value format: expected valid password format'
}
}
CustomString util class extends String with additional validation layer:
try {
const password = new Password(69)
} catch (error) {
console.log(error.name) // ValidationError
console.log(error.message) // Incorrect value type - Password: expected valid String type
}
You can use ValidationError to keep unified error handling flow
const {Uri, ValidationError} = require('typeteca')
const uri = new Uri('https://example.com')
if (uri.includes('https'))
throw new ValidationError('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.