{"id":16642231,"url":"https://github.com/flcwl/validater","last_synced_at":"2025-07-01T13:07:10.054Z","repository":{"id":42682450,"uuid":"316685390","full_name":"Flcwl/validater","owner":"Flcwl","description":"A excellent and lightweight JavaScript validation library, it works better with the Validator.js.","archived":false,"fork":false,"pushed_at":"2023-03-05T16:53:09.000Z","size":625,"stargazers_count":1,"open_issues_count":11,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-01T13:06:02.356Z","etag":null,"topics":["form","react-hooks","validater","validation","validator"],"latest_commit_sha":null,"homepage":"","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/Flcwl.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-28T08:01:17.000Z","updated_at":"2023-03-04T04:23:14.000Z","dependencies_parsed_at":"2025-04-05T01:27:50.040Z","dependency_job_id":"776e83d9-35c7-4cbd-b426-8e72dea82029","html_url":"https://github.com/Flcwl/validater","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":"Flcwl/duration-pretty","purl":"pkg:github/Flcwl/validater","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flcwl%2Fvalidater","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flcwl%2Fvalidater/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flcwl%2Fvalidater/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flcwl%2Fvalidater/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Flcwl","download_url":"https://codeload.github.com/Flcwl/validater/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flcwl%2Fvalidater/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262969885,"owners_count":23392529,"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":["form","react-hooks","validater","validation","validator"],"created_at":"2024-10-12T07:49:08.372Z","updated_at":"2025-07-01T13:07:10.022Z","avatar_url":"https://github.com/Flcwl.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Validater\n\n[![Build Status](https://travis-ci.org/Flcwl/validater.svg?branch=master)](https://travis-ci.org/github/Flcwl/validater)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Flcwl/validater/blob/master/LICENSE)\n[![npm version](https://img.shields.io/npm/v/@flcwly/validater.svg?style=flat)](https://www.npmjs.com/package/@flcwly/validater)\n\n\u003e An excellent and useful JavaScript validation library, it works better with the Validator.js.\n\n---\n\n## Getting Started\n\n### Installation\n\n```console\nnpm install --save @flcwly/validater\n```\n\n### Documentation\n\nYou can Using the `Validater` to check any data, like `string` and others.\n\nFirstly, you need extend the Validator plugins as your need.\n\n```js\nconst Validater = require('@flcwly/validater').default\nconst requiredPlugin = (value: any, strategy = true) =\u003e {\n  return strategy \u0026\u0026 !!value\n}\nconst maxPlugin = (value: string, strategy: number) =\u003e {\n  return required(value) \u0026\u0026 value.length \u003c= strategy\n}\n\n// extend\nValidater.extend('required', requiredPlugin).extend('max', maxPlugin)\n```\n\nSecondly, you can Using the `required` and `max` like the following way.\n\n```js\nconst v = new Validater([\n  {\n    name: 'required',\n    strategy: true,\n    message: 'please enter your name.',\n  },\n  {\n    name: 'max',\n    strategy: 11,\n    message: 'Please enter the correct name.',\n  },\n])\nconst errorMsg = v.validateOne('abc1234567890') // \"Please enter the correct name.\"\n```\n\nYou can define the order of validators by array.\n\nor using ES6 Module:\n\n```js\nimport Validater from '@flcwly/validater'\n```\n\n### Constructor\n\nThe Validater constructor accepts two parameters: `new Validater(rules, options)`.\n\n- `rules: ValidaterRule[]`\n\nA array that type is `ValidaterRule`.\n\n```js\nname: string      // extend ruleName for validation\nstrategy: any     // extend strategy for validation\nmessage?: string  // rule error message\n```\n\n- `options`\n\n```js\ntype: 'string' | 'boolean' | 'number' // transform type before validating, default is `string`\ntrim: boolean // trim(value) before validating when type is string, default is `true`\ndefaultMessage: string // default global error message, default is `The value is incorrect`,\n```\n\nIf you have not set a specific error message for rule, the error message will using `defaultRuleMessage`.\n\n```js\nconst defaultRuleMessage = 'required defaultRuleMessage'\nValidater.extend('required', requiredPlugin).extend('max', maxPlugin, defaultRuleMessage)\n\nconst v = new Validater([\n  {\n    name: 'required',\n    strategy: true,\n  },\n])\nconst errorMsg = v.validateOne('') // \"required defaultRuleMessage\"\n```\n\nIf you don't set a `defaultRuleMessage` when `extend`, Then will using the `defaultMessage`.\n\n```js\nconst v = new Validater([\n  {\n    name: 'required',\n    strategy: true,\n  },\n])\nconst errorMsg = v.validateOne('') // \"The value is incorrect\"\n```\n\nThe message string's relationship for overriding is:\n\n```console\nmessage \u003e defaultRuleMessage \u003e defaultMessage\n```\n\nAnd the message string is parsed and replaced with the value by \\$0.\n\n```js\nconst v = new Validater([\n  {\n    name: 'numeric',\n    strategy: true,\n    message: '$0 error',\n  },\n])\nconst errorMsg = v.validateOne('1a1') // \"1a1 error\"\n```\n\n---\n\n### Instance Functions\n\n- `addRules: (rules?: ValidaterRule[] | undefined) =\u003e void;`\n\nTraverse rules to register validators generated based on rules.\n\n- `validate: (value: unknown, ruleName?: string | undefined) =\u003e string | void;`\n\nValidate a value with special ruleName.\n\n- `validateOne: (value: unknown) =\u003e string | void;`\n\nValidate a value base on array order starts at 0.\n\n- `validateAll: (value: unknown) =\u003e this;`\n\nValidate a value for all rules, then will return `this`.\n\n- `hasError: () =\u003e boolean;`\n\nCheck if the error exists, return `true` when error.\n\n- `getError: (ruleName?: string | undefined) =\u003e any;`\n\nGet error to special ruleName, return all error as object when `ruleName === undefined`.\n\n---\n\n## With validator.js\n\nIt works better with the [Validator.js](https://github.com/validatorjs/validator.js).\n\n```js\nimport Validator from 'validator'\n\nValidater.extend('isEmail', Validator.isEmail)\n\nconst v = new Validater([\n  {\n    name: 'isEmail',\n    message: '\"$0\" error',\n  },\n])\nconst errorMsg = v.validateOne('@mail.com') // \"\"@mail.com\" error\"\n```\n\n---\n\n## With React Hooks\n\nYou can use `Validater` with React hooks like the following usage. Here is a [CodeSandbox Demo](https://codesandbox.io/s/flcwlyvalidater-demo-g0822?file=/src/App.tsx).\n\n```tsx\nimport React, { useState, useCallback, useMemo } from 'react'\nimport Validater from '@flcwly/validater'\nimport Validator from 'validator'\n\nconst requiredPlugin = (value: any, strategy = true) =\u003e {\n  return strategy \u0026\u0026 !Validator.isEmpty(value)\n}\nconst patternPlugin = (value: string, strategy: RegExp) =\u003e {\n  return requiredPlugin(value) \u0026\u0026 strategy.test(value)\n}\n\nValidater.extend('required', requiredPlugin).extend('pattern', patternPlugin)\n\nexport const useValidater = (initialValue: any, rules: any[]) =\u003e {\n  const [value, setValue] = useState(initialValue)\n  const [error, setError] = useState()\n  const [verified, setVerified] = useState(!rules)\n  const validater = useMemo(() =\u003e new Validater(rules), [rules])\n\n  const verify = useCallback(\n    (val = value) =\u003e {\n      const errorMsg = validater.validateOne(val)\n      setVerified(true)\n      setError(errorMsg)\n      return errorMsg\n    },\n    [value, setError, setVerified, validater]\n  )\n\n  return [value, setValue, error, verify, verified]\n}\n\nexport function InputTestComponent() {\n  const [phone, setPhone, phoneError, verifyPhone, hasVerifiedPhone] = useValidater('', [\n    {\n      name: 'required',\n      strategy: true,\n      message: 'Please enter the phone number',\n    },\n    {\n      name: 'pattern',\n      strategy: /^[\\d]{11}$/,\n      message: 'Please enter the correct phone number',\n    },\n  ])\n  const btnDisabled = useMemo(() =\u003e {\n    return !!phoneError || !hasVerifiedPhone\n  }, [phoneError, hasVerifiedPhone])\n  const handleSubmit = useCallback(async () =\u003e {\n    const phoneErrorMsg = phoneError || verifyPhone()\n    if (phoneErrorMsg) {\n      // deal with error here...\n    }\n    // request to submit\n  }, [phoneError, verifyPhone])\n\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003e\n        Phone number:\n        \u003cinput\n          value={phone}\n          onChange={(e) =\u003e {\n            const val = e.target.value\n            setPhone(val)\n            verifyPhone(val)\n          }}\n          onBlur={() =\u003e verifyPhone()}\n        /\u003e\n      \u003c/div\u003e\n      \u003cdiv\u003e\n        Error:\n        \u003cspan\u003e{hasVerifiedPhone ? phoneError || 'No Error.' : 'Initial Status.'}\u003c/span\u003e\n      \u003c/div\u003e\n      \u003cbutton disabled={btnDisabled} onClick={handleSubmit}\u003e\n        Submit\n      \u003c/button\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n## Tests\n\nYou can find all cases in `files:/test/*.spec.js`, And testing Using below script.\n\n```console\nnpm run test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflcwl%2Fvalidater","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflcwl%2Fvalidater","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflcwl%2Fvalidater/lists"}