{"id":15674135,"url":"https://github.com/viczam/easevalidation","last_synced_at":"2025-05-06T20:46:23.288Z","repository":{"id":33499497,"uuid":"158287686","full_name":"viczam/easevalidation","owner":"viczam","description":"javascript validation library","archived":false,"fork":false,"pushed_at":"2023-01-06T01:51:49.000Z","size":2247,"stargazers_count":14,"open_issues_count":21,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-17T13:54:43.392Z","etag":null,"topics":["javascript-library","validation"],"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/viczam.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":"2018-11-19T20:44:45.000Z","updated_at":"2023-07-27T12:04:34.000Z","dependencies_parsed_at":"2023-01-15T01:16:30.845Z","dependency_job_id":null,"html_url":"https://github.com/viczam/easevalidation","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viczam%2Feasevalidation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viczam%2Feasevalidation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viczam%2Feasevalidation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viczam%2Feasevalidation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/viczam","download_url":"https://codeload.github.com/viczam/easevalidation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252769132,"owners_count":21801373,"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":["javascript-library","validation"],"created_at":"2024-10-03T15:43:39.854Z","updated_at":"2025-05-06T20:46:23.246Z","avatar_url":"https://github.com/viczam.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# easevalidation\n\n### Install\n\n```\nnpm install easevalidation\n```\n\n`easevalidation` is an easy to extend javascript validation library that supports multiple types of validators:\nfunctional validation, schema based, chained validators etc.\n\nIt comes bundled with all lodash is\\* validators (like isPlainObject, isFinite etc), all the [validator.js](https://github.com/chriso/validator.js) validators\nand the date-related validators [date-fns](https://date-fns.org/) comes with.\nOn top of that, it features some commonly used validators you can find [here](https://github.com/viczam/easevalidation/tree/master/src/validators).\n\nYou test the input data using the `test` function:\n\n```js\nimport { test, validators } from 'easevalidation';\n\nconst { isNumber, isMin, isMax } = validators;\n\nconst isValid = test(isNumber(), isMin(10), isMax(15))(13); // true\n```\n\nOr you can use a chained validator:\n\n```js\nimport { test, number } from 'easevalidation';\n\nconst isValid1 = test(\n  number()\n    .isMin(10)\n    .isMax(15),\n)(13);\n\n// or\n\nconst isValid2 = number()\n  .isMin(10)\n  .isMax(15)\n  .test(13);\n\n// isValid1 === isValid2 === true\n```\n\nYou can also validate an object by a schema:\n\n```js\nimport { test, validators } from 'easevalidation';\n\nconst { isSchema, isEmail, isRequired, isString, isLength } = validators;\n\nconst schema = isSchema({\n  email: [isEmail()],\n  password: [isRequired(), isString(), isLength({ min: 5 })],\n});\n\nconst isValid = test(schema)({\n  email: 'me@gmail.com',\n  password: '12345',\n});\n```\n\nOr:\n\n```js\nimport { test, validators } from 'easevalidation';\n\nconst { isPlainObject, isProperty, isEmail, isRequired, isString, isLength } = validators;\n\nconst schema = [\n  isPlainObject(),\n  isProperty('email', isEmail()),\n  isProperty('password', isRequired(), isString(), isLength({ min: 5 })),\n];\n\nconst isValid = test(schema)({\n  email: 'me@gmail.com',\n  password: '12345',\n});\n```\n\nWhile `easevalidation` comes prebuilt with many validators, creating your own validators is an easy job.\n\n```js\nimport { createValidator, test } from 'easevalidation';\n\nconst isBetween = createValidator('isBetween', (value, min, max) =\u003e min \u003c= value \u0026\u0026 value \u003c= max);\n\nconst isValid = test(isBetween(10, 15))(13); // true\n```\n\nValidators can also update the value they receive for testing.\n\n```js\nimport { createValidator, test } from 'easevalidation';\nimport { ObjectID as objectId } from 'mongodb';\n\nconst isObjectId = createValidator(\n  'isObjectId',\n  value =\u003e objectId.isValid(value),\n  value =\u003e objectId(value),\n);\n\nconst isValid = test(isObjectId())('5bf6cd3e766582a5bf892519');\n```\n\nAs you can see, the signature of `createValidator` is:\n\n```js\ncreateValidator(String code, Function validate, [Function updateValue, Function validateConfig])\n```\n\nKeep in mind that `updateValue` will run **after** `validate` function tests the value and **only** if it returns true (`value` passes validation).\n\nOnly `code` and `validate` are required, the rest of arguments are optional.\n`validateConfig` is a function that can be used to validate the configuration the `validate` function will receive.\n\nAnother way to update the value is by returning an object from `validate`:\n\n```js\nimport { createValidator, test } from 'easevalidation';\nimport { ObjectID as objectId } from 'mongodb';\n\nconst isObjectId = createValidator('isObjectId', value =\u003e ({\n  isValid: objectId.isValid(value),\n  value: objectId(value),\n}));\n\nconst isValid = test(isObjectId())('5bf6cd3e766582a5bf892519');\n```\n\nSometimes you may want to get access to the final updated value, besides just testing it.\n\n```js\nimport { createValidator, test, validators } from 'easevalidation';\nimport { ObjectID as objectId } from 'mongodb';\n\nconst { isSchema, isString, isNumber, isMin } = validators;\n\nconst isObjectId = createValidator('isObjectId', value =\u003e ({\n  isValid: objectId.isValid(value),\n  value: objectId(value),\n}));\n\nconst validate = test(\n  isSchema({\n    name: isString(),\n    age: [isNumber(), isMin(20)],\n    id: isObjectId(),\n  }),\n);\n\nconst isValid = validate({\n  name: 'John Doe',\n  age: '22',\n  id: '5bf6cd3e766582a5bf892519',\n});\n\nconst { value } = validate;\n\n// In this case `isValid` will be `true` and `value` will be:\n\n{\n  name: 'John Doe',\n  age: 22, // number\n  id: ObjectId('5bf6cd3e766582a5bf892519') // object\n}\n```\n\nInstead of building a validation function like we did above, you can use `validate`:\n\n```js\nimport { createValidator, validate, validators } from 'easevalidation';\nimport { ObjectID as objectId } from 'mongodb';\n\nconst { isSchema, isString, isNumber, isMin } = validators;\n\ntry {\n  const value = validate(\n    isSchema({\n      name: isString(),\n      age: [isNumber(), isMin(20)],\n      id: isObjectId(),\n    }),\n  )({\n    name: 'John Doe',\n    age: '22',\n    id: '5bf6cd3e766582a5bf892519',\n  });\n} catch (err) {\n  // won't get here, because it passes validation\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviczam%2Feasevalidation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fviczam%2Feasevalidation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviczam%2Feasevalidation/lists"}