Inspired in Flunt, Every app has business rules and validations, and you probably will need to keep all the errors and notifications that happened and send it to somewhere, maybe to your UI.
Flunt implements the Notification Pattern and helps you to track everything that happend, consolidating your notifications and making it easy to access and manipulate.
$ npm install @crowrvo/plume --save
import { Notifiable, Contract } from "@crowrvo/plume";
// extends your class to Notifiable
class Customer extends Notifiable {
constructor(name, lastname, age) {
super();
this.name = name;
this.lastname = lastname;
this.age = age;
this.AddNotification(
new Contract()
.Equal(name.length > 0, true, "Name", "name is required")
.Equal(lastname.length > 0, true, "Lastname", "Lastname is required")
.Equal(age > 18, true, "Age", "You need more than 18 years")
);
}
}
// instance class
var customer = new Customer(null, "Goncalves", 10);
// take your messages
console.log(customer.GetMessages);
//take all notifications
console.log(customer.GetNotifications);
const { Notifiable, Contract } = require("@crowrvo/plume");
class Customer extends Notifiable {
constructor(name, lastname, age) {
super();
this.name = name;
this.lastname = lastname;
this.age = age;
this.AddNotification(
new Contract()
.Equal(name.length > 0, true, "Name", "name is required")
.Equal(lastname.length > 0, true, "Lastname", "Lastname is required")
.Equal(age > 18, true, "Age", "You need more than 18 years")
);
}
// You can also add notifications in methods or gets/sets
public newAge(newAge: number) {
this.AddNotification(
new Contract()
.Equal(age > 18, true, "Age", "You need more than 18 years")
.Equal(age < 200, true "Age", "You need to inform a valid age")
);
this.age = newAge;
}
}
// To use class
module.exports = Customer;
// instance class
var customer = new Customer(null, "Goncalves", 10);
// take your messages
console.log(customer.GetMessages);
//take all notifications
console.log(customer.GetNotifications);
Follow the methods that the library provides.
Validations methods.
--
Example
Equal(value, comparer, property, message);
StrictEqual(value, comparer, property, message);
NotEqual(value, comparer, property, message);
StrictNotEqual(value, comparer, property, message);
//To get messages and notifications
GetNotifications();
GetMessages();
// To know if is valid
isValid();
Class turn another class notifiable and works with flunt.
--
Example
AddNotification(property, message);
AddNotification(Notification);
AddNotification(Notification[]);
AddNotification(contract);
// To get messages
GetMessages();
// To know if is valid
isValid();
We use SemVer for versioning. For the versions available, see the tags on this repository.
Crowrvo |
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details