2.0.2 • Published 6 years ago

lot-validate v2.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

lot-validate

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;
	}