lot-validate

2.0.2 • Public • Published

lot-validate

Best way to validate your mongo Schemas and best practices of response API.

npm download

Features

Focused on backend, lot-validate has the following characteristics

  • Response standardization
  • Fast development
  • Validation of mongo Schemas

How to use

Installing

Node.js

npm i lot-validate

Importing

const { DefaultResponse, SchemaValidate  } = require("lot-validate")

Using

For response with error

   var  response  =  new  DefaultResponse();
   if(1 != 2)
     response.addErro("Different number", "number_field");

Response:

   {
    "errors": [
        {
            "field": "number_field",
            "error": "Different number"
        }
    ],
    "messageOk": "",
    "data": {},
    "hasError": true
   }

For response with success

   var response = SchemaValidate.Validate(User, _req.body);
   if (!response.hasError){
       var dataUser = await User.create(_req.body)
       response.success("Salvo com sucesso!", dataUser);
   }
   return response;

Response:

   {
   "errors": [],
   "messageOk": "Salvo com sucesso!",
   "data": {
       "_id": "5b11da28775d7939748f7d47",
       "email": "eliveltongama@hotmail.com",
       "password": "123456",
       "username": "elivelton.gama"
   },
   "hasError": false
   }

Validating mongo Schema

   public DefaultResponse validating(myObjectRequest)
    {
            return SchemaValidate.Validate(mongoSchema, myObjectRequest);
    }

Return: object DTO of Response (DefaultResponse)

Class

Below is the return class and its attributes

DefaultResponse

    class DefaultResponse {
        errors: ErrorsResponse[]
        messageOk: string;
        data: {}
        hasError: boolean;
    }

ErrorsResponse

    class ErrorsResponse {
        field : string
        error: string;
    }

Package Sidebar

Install

npm i lot-validate

Weekly Downloads

9

Version

2.0.2

License

MIT

Unpacked Size

8.78 kB

Total Files

7

Last publish

Collaborators

  • eligama