1.4.8 • Published 10 months ago

flunt-notifications v1.4.8

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

Flunt

Flunt is a fluent approach to using the Notification pattern with your TypeScript entities, centralizing all the changes you've made and making them easy to access when needed. It is built on top of the https://github.com/andrebaltieri/Flunt repository and supports generic features. With Flunt, you can easily handle validation notifications, aggregating all relevant information in one place for more efficient handling of your data.

Models

When I install some library, I have difficulties in understanding the models/entities. So it was with that in mind that I decided to create this section especially to detail Flunt's main classes.

How to install

It's very simple, you just need to install the library with NPM

npm i flunt-validations

Examples

Validating a User with the Default Notification

import { Notification, Notifiable, Contract } from "flunt-validations";

class User extends Notifiable { // Class inheriting from "Notifiable"

  constructor(
      public name: string,
      public email: string,
      public password: string
  ) {
    super();

    // Creating the contract to configure the validation schemes.
    let contract = new Contract()

    // Validation scheme -> The "name" field must be more than 3 characters
    contract.minLength(name, 3, new Notification("name", "Name must have more than 3 chars"));
    // Validation scheme -> The "email" field must have the pattern of an email
    contract.isEmail(email, new Notification("email", "E-mail must be valid."));
    // Validation scheme -> The "password" field must be more than 8 characters
    contract.minLength(password, 8, new Notification("password", "Password must have more than 8 chars"));

    // Finally, sending all fields from the constructor to the Notifiable class,
    // which will apply the validation schemes.
    super.AddNotifications(contract)
  }
}



const user = new User("o", "gmail", "123") // creating an invalid user.
console.log(user.allFieldsValid()) // false
console.log(user.getNotifications()) // there will be an array with three notification,
                                     // because the three fields are invalid

const errors = user.getNotifications();
console.log(errors[0].property) // name
console.log(errors[1].property) // email
console.log(errors[2].message) // Password must have more than 8 chars


const user2 = new User("Matheus", "mxtheuz@gmail.com", "12345678") // creating a valid user.
console.log(user2.allFieldsValid()) // true
console.log(user2.getNotifications()) // [] -> nothing, because all fields are valid.
import { Notification, Notifiable, Contract } from "flunt-validations";

class CustomNotification extends Notification {
  constructor(
      public customField: string // custom field
  ) {
    super();
  }

}

class User extends Notifiable<CustomNotification> { // Class inheriting from "Notifiable"

  constructor(
    public name: string,
    public email: string,
    public password: string
  ) {
    super();

    // Creating the contract to configure the validation schemes.
    let contract = new Contract<CustomNotification>()

    // Validation scheme -> The "name" field must be more than 3 characters
    contract.minLength(name, 3, new CustomNotification("name", "Name must have more than 3 chars", "notificationnnn"));
    // Validation scheme -> The "email" field must have the pattern of an email
    contract.isEmail(email, new CustomNotification("email", "E-mail must be valid.", "notificationnnn"));
    // Validation scheme -> The "password" field must be more than 8 characters
    contract.minLength(password, 8, new CustomNotification("password", "Password must have more than 8 chars", "notificationnnn"));

    // Finally, sending all fields from the constructor to the Notifiable class,
    // which will apply the validation schemes.
    super.AddNotifications(contract)
  }
}


const user = new User("o", "gmail", "123") // creating an invalid user.
console.log(user.allFieldsValid()) // false
console.log(user.getNotifications()) // there will be an array with three notification,
// because the three fields are invalid

const errors = user.getNotifications();
console.log(errors[0].property) // name
console.log(errors[1].customField) // notificationnnn
console.log(errors[2].message) // Password must have more than 8 chars


const user2 = new User("Matheus", "mxtheuz@gmail.com", "12345678") // creating a valid user.
console.log(user2.allFieldsValid()) // true
console.log(user2.getNotifications()) // [] -> nothing, because all fields are valid.

Authors

Matheus Barichello Piccoli - https://github.com/ofmxtheuuz

License

This project is licensed under the MIT License.

1.4.8

10 months ago