UNPKG

simple-body-validator

Version:

This package is inspired by Laravel validation, and aims to make body validation easier for Javascript developers

60 lines (59 loc) 1.33 kB
'use strict'; import Lang from '../lang'; import { deepFind } from '../utils/object'; export default class RuleContract { constructor() { /** * The validation error message. */ this.message = ''; /** * All of the data under validation. */ this.data = {}; } /** * Determine if the validation rule passes. */ passes(value, attribute) { return true; } ; /** * Get the validation error message. */ getMessage() { return this.message; } ; /** * Set the data under validation. */ setData(data) { this.data = data; return this; } ; /** * Set the tranlation language */ setLang(lang) { this.lang = lang; return this; } ; /** * Get the translated error message based on the specified path */ trans(path, params = {}) { const validatonMessages = Lang.get(this.lang); let message = deepFind(validatonMessages, path) || ''; if (!message) { return message; } for (let key in params) { message = message.replace(`:${key}`, params[key]); } return message; } }