Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

193 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

A package to handle user inputs automatically





Table of content

The content below may not correspond to the structure of the objects

NEW   Default SchemaTypes are now completely seperate from the original package!
See more at @protagonists/coerce-basics





How to use?

Description

This package takes in an object and coerces it into the same structure as the passed model
This can be a very usefull tool to manage user inputed data automatically


Import

Terminal

npm install @protagonists/coerce

Node.js

const Coerce = require("@protagonists/coerce");

Example

Code:

// Imports
const { Schema } = require("@protagonists/coerce");
const { StringType, IntRange, DateType } = require("@protagonists/coerce-basics");

// Create schema 'Person'
const Person = new Schema({
  name: StringType,
  age: IntRange(0, 200),
  birthday: DateType,
  friends: [StringType]
});

// Coerce object into following the model
const John = new Person({
  name: "John",
  favColor: "Red",
  friends: ["Steve", "Carla", "Meep"],
  kids: 2,
  birthday: "1984",
  age: 37
});

// Log the result
console.log(John);

Output:

{
  name: 'John',
  age: 37,
  birthday: 1984-01-01T00:00:00.000Z,
  friends: [ 'Steve', 'Carla', 'Meep' ]
}






This is the bottom, there is nothing more.
Go back up?

About

A package to handle object coercing!

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages