{"id":21156842,"url":"https://github.com/lkiarest/validator","last_synced_at":"2025-03-14T15:23:39.294Z","repository":{"id":57389415,"uuid":"103523791","full_name":"lkiarest/validator","owner":"lkiarest","description":"common js validator","archived":false,"fork":false,"pushed_at":"2017-10-10T06:49:01.000Z","size":61,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-23T18:37:03.808Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lkiarest.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-14T11:22:58.000Z","updated_at":"2017-09-14T11:32:04.000Z","dependencies_parsed_at":"2022-09-09T19:24:40.395Z","dependency_job_id":null,"html_url":"https://github.com/lkiarest/validator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lkiarest%2Fvalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lkiarest%2Fvalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lkiarest%2Fvalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lkiarest%2Fvalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lkiarest","download_url":"https://codeload.github.com/lkiarest/validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243598384,"owners_count":20316951,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T11:52:58.875Z","updated_at":"2025-03-14T15:23:39.259Z","avatar_url":"https://github.com/lkiarest.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A light weight js object validator\n\n[![Build Status](https://travis-ci.org/lkiarest/validator.svg?branch=master)](https://travis-ci.org/lkiarest/validator)\n[![npm](https://img.shields.io/badge/npm-v1.0.4-red.svg)](https://www.npmjs.com/package/uvalidator)\n[![npm](https://img.shields.io/npm/l/express.svg)](https://github.com/lkiarest/validator/blob/master/LICENSE)\n\nVery easy to use and extend check rules of your own\n\n- normal validation\n- async validation\n- user defined validation\n- dynamic validation\n\n### supported rules (on development)\nemail, max, maxlength, min, minlength, number, regex, required\n\n### install\n```shell\n    npm install uvalidator # yarn add uvalidator\n```\n\n### basic usage\n```javascript\n    import validator from 'uvalidator'\n\n    // if we have a 'user' property to check\n    const rules = {\n        user: {\n            rule: 'required', // pre-defined rule\n            errorMsg: 'user is required'\n        }\n    }\n\n    let result = validator.validate({\n        user: 'lily'\n    }, rules)\n    // ok !\n    result.ok() === true\n\n    result = validator.validate({\n        user: ''\n    }, rules)\n    // failed !\n    result.ok() === false\n    result.getErrors() = [{name: 'user', message: 'name is required'}]\n```\n\n### with dynamic params\n```javascript\n    const rules = {\n        username: {\n            rule: 'maxlength',\n            params: 5,\n            errorMsg: 'username is too long'\n        }\n    }\n\n    // return {success: false, errors: [{name: 'username', messag: 'username is too long'}]}\n    validator.validate({\n        username: 'aaaaaa'\n    }, rules)\n```\n\n### user defined rule\n```javascript\n    /// your can register several rules in an array either\n    validator.register({ // add a rule named 'contain' to check sub string\n        name: 'contain',\n        validate: function(val, str) {\n            return val \u0026\u0026 val.indexOf(str) \u003e -1\n        }\n    })\n\n    const rules = {\n        desc: {\n            rule: 'contain',\n            params: 'hello',\n            errorMsg: 'your description must contain hello'\n        }\n    }\n\n    // {success: true}\n    const result = validator.validate({\n        desc: 'hello world'\n    }, rules)\n```\n\n### add dynamic rule\n```javascript\n    const rules = {\n        name: {\n            rule: 'required',\n            errorMsg: 'name is required'\n        },\n        desc: {\n            rule: function(val) { // define the check funtion\n                return val.indexOf('hello') \u003e -1\n            },\n            errorMsg: 'your description must contain hello'\n        }\n    }\n\n    const result = validator.validate({\n        name: '',\n        desc: 'hello world'\n    }, rules)\n```\n\n### async validation\n```javascript\n    const rules = {\n        name: {\n            rule: function(val) {\n                return new Promise(function(resolve) {\n                    // ... send ajax request to check name\n                    resolve(false) // check failed\n                })\n            },\n            errorMsg: 'this name has been used'\n        }\n    }\n\n    // the 'validate' method will return a Promise while any rule is async,\n    // and the errors (of sync or async rules) will be resolved by order\n    validator.validate({\n        name: 'aaa'\n    }, rules).then(result =\u003e {\n        result.ok() // false\n        result.getErrors() // [{name: 'name', message: 'this name has been used'}]\n    })\n```\n\n### About Result Object\n```\nresult.ok() // true or false\nresult.getErrors() // Array of errors, each error has name and message property\nresult.getFirstError() // the first error object, has name and message property\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flkiarest%2Fvalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flkiarest%2Fvalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flkiarest%2Fvalidator/lists"}