hg-validator

所属分类:数据结构
开发工具:TypeScript
文件大小:0KB
下载次数:0
上传日期:2021-07-11 16:22:29
上 传 者sh-1993
说明:  一个简单的模式结构javascript数据验证器。
(A simple, schema-structured javascript data validator.)

文件列表:
hg-validator-dev-v1/ (0, 2021-07-11)
hg-validator-dev-v1/package-lock.json (389, 2021-07-11)
hg-validator-dev-v1/package.json (669, 2021-07-11)
hg-validator-dev-v1/src/ (0, 2021-07-11)
hg-validator-dev-v1/src/index.ts (106, 2021-07-11)
hg-validator-dev-v1/src/main/ (0, 2021-07-11)
hg-validator-dev-v1/src/main/HGValidator.ts (7705, 2021-07-11)
hg-validator-dev-v1/src/main/ResponseHandler.ts (101, 2021-07-11)
hg-validator-dev-v1/src/utils/ (0, 2021-07-11)
hg-validator-dev-v1/src/utils/errors.ts (195, 2021-07-11)
hg-validator-dev-v1/src/utils/functions.ts (514, 2021-07-11)
hg-validator-dev-v1/src/utils/index.ts (52, 2021-07-11)
hg-validator-dev-v1/tsconfig.json (5846, 2021-07-11)

## INTRO A simple, schema-structured javascript data validator. Can be easily used both client side and server side. This is created as a generic and independent validation solution for use in any context. Works in any typescript and javascript project. # **Installation** ```javascript npm i hgvalidator --save ``` # **Usage** #### **Import the module** ```javascript import Validator from 'hgvalidator' ``` #### Some data to validate (These may have been extracted directly from forms or sent as request body to the server) ```javascript let title = ' ' let content = 'Some long wysiwyg content' let username = user123 let random = 78 // A variable to store the validation errors, if any.. let validationErrors = null ``` #### Every validation requires a SCHEMA which must be an array of objects like in the example below. ```javascript const schema = [ { fieldName: 'Title', //Required as custom reference to the validated field. data: title, //The data to validate rules: { required: true, string: true, max: 200 // The min/max rules work for both strings (returns characters length), and numbers (returns digit size). }, messages: { //Optional. Add some custom validation messages if necessary. max: 'Title should not be more than 200 characters' } }, { fieldName: 'Content', data: content, rules: { required: true, }, }, { fieldName: 'Username', data: username, rules: { required: true, pattern: /^([a-zA-Z]{4,})([0-9])*$/ }, message:{ pattern: 'blah blah blah' } }, { fieldName: 'Random', data: random, rules: { required: true, min: 100 }, } ] ``` **[See all default supported validation rules](https://github.com/dayo4/hg-validator/blob/master/#Supported_Rules)** #### **Validate the data** ```javascript const validated = Validator.validate(schema, { skip: ['Content'] }) /* The "skip" option prevents validation of the fields names listed in the array. This is Useful when using same schema for multiple validations */ if(validated) { console.log('success') // preceed with other stuffs } else { validationErrors = Validator.getErrors() // Validator.getErrors() returns an object (contaning the first error of each validated field) by default.. // Result: { Title: "Title is required and cannot be empty", Random: "Random value should not be less than 100", } //If you'd like to get back only one error, use the {"format:'single'}" option. validationErrors = Validator.getErrors({format: 'single'}) // Result: "Title is required and cannot be empty" } ``` #### **Then in the html (if validating a form)** ```html {{ titleErrorMsg }} ... ... ... ... ``` #### **Available utility methods** ```javascript - Validator.validate(schema, options = {}) //This has been explained above - Validator.sanitize(data, options = {}) // for personally stripping out all html tags and trimming empty spaces in data. /*Sanitize options */ const options = { strict: false // defaults to 'true'. /* If false, only '