{"id":13657296,"url":"https://github.com/coditorium/nodejs-fluent-validator","last_synced_at":"2025-04-12T10:09:54.734Z","repository":{"id":25030197,"uuid":"28449736","full_name":"coditorium/nodejs-fluent-validator","owner":"coditorium","description":"Fluent validation for node.js","archived":false,"fork":false,"pushed_at":"2015-09-30T16:51:12.000Z","size":228,"stargazers_count":17,"open_issues_count":3,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-21T17:09:09.424Z","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/coditorium.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":"2014-12-24T13:27:24.000Z","updated_at":"2023-02-09T19:06:41.000Z","dependencies_parsed_at":"2022-09-05T08:21:36.901Z","dependency_job_id":null,"html_url":"https://github.com/coditorium/nodejs-fluent-validator","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coditorium%2Fnodejs-fluent-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coditorium%2Fnodejs-fluent-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coditorium%2Fnodejs-fluent-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coditorium%2Fnodejs-fluent-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coditorium","download_url":"https://codeload.github.com/coditorium/nodejs-fluent-validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248312175,"owners_count":21082637,"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-08-02T05:00:40.291Z","updated_at":"2025-04-12T10:09:54.715Z","avatar_url":"https://github.com/coditorium.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# fluent-validator\n\n[![Travis build status](https://travis-ci.org/coditorium/nodejs-fluent-validator.png?branch=master)](https://travis-ci.org/coditorium/nodejs-fluent-validator)\n[![dependencies](https://david-dm.org/coditorium/nodejs-fluent-validator.png)](https://david-dm.org/coditorium/nodejs-fluent-validator)\n[![Coverage Status](https://coveralls.io/repos/coditorium/nodejs-fluent-validator/badge.svg)](https://coveralls.io/r/coditorium/nodejs-fluent-validator)\n\n[![NPM info](https://nodei.co/npm/fluent-validator.png?downloads=true)](https://www.npmjs.com/package/fluent-validator)\n\nFluent validator that enables validation on multiple parameters at once.\n\n## Chained validation\n\n### Express paginated endpoint\n\nExample with express paginated endpoint.\nBoth query parameters (page and size) must be positive integers or empty values.\n\n``` javascript\nvar validator = require('fluent-validator');\n\napp.get('/users', function() {\n\tvalidator()\n\t\t.validate(req.query.page).param('page').isInt().and.isPositive().or.isEmpty()\n\t\t.validate(req.query.size).param('size').isInt().and.isPositive().or.isEmpty()\n\t\t.throwOnError();\n\n\t// query params are valid\n});\n```\n\n### Separating validation rules\n\n**OR Separator**\n``` javascript\n// Example: chain1.or.chain2\nvalidator().validate(value).isInt().or.isEmpty();\n```\n\n- if `value` passes validation `chain1` than `chain2` is omitted and validation passes successfully.\n- if `value` does not pass validation `chain1` than `chain2` is checked.\n- if `value` does not any of validation chains then errors are produced from `chain1`.\n\n\n**AND Separator**\n\n``` javascript\n// Example: chain1.and.chain2\nvalidator().validate(value).isInt().and.isPositive().isDivisibleBy(2);\n```\n\n- if `value` passes validation `chain1` than `chain2` is checked as well.\n- if `value` does not pass validation `chain1` than `chain2` is omitted and errors are produced from `chain1`.\n\n### Result checking\n\nYou can react on validation result in multiple ways:\n``` javascript\nvar validation = validator()\n\t.validate(req.query.page).isInt().and.isPositive().or.isEmpty()\n\t.validate(req.query.size).isInt().and.isPositive().or.isEmpty();\n\nvalidation.getErrors(); // Returns array of validation errors\nvalidation.hasErrors(); // Returns true if there are validation errors\nvalidation.check(); // Returns true if there are no validation errors\nvalidation.throwOnError(); // Throws error if there are validation errors.\n```\n\n### Simple shortcut\n\nYou can shorten validation chain:\n``` javascript\nvar validation1 = validator().validate(req.query.page).isInt().and.isPositive().or.isEmpty();\nvar validation2 = validator(req.query.page).isInt().and.isPositive().or.isEmpty();\n\n// validation2 is just a shorter version of validation1\n\n```\n\n## Simple validation\n\nValidation without chaining.\n\n``` javascript\nvar validator = require('fluent-validator');\n\n// isPositive: just executes checks if input \u003e 0\nvalidator.isPositive(1);\t\t// true\nvalidator.isPositive(-1);\t\t// false\nvalidator.isPositive(\"1\");\t\t// true\nvalidator.isPositive(\"-1\");\t\t// false\nvalidator.isPositive(0.1);\t\t// true\nvalidator.isPositive(-0.1);\t\t// false\nvalidator.isPositive(\"0.1\");\t// true\nvalidator.isPositive(\"-0.1\");\t// false\nvalidator.isPositive({});\t\t// false\nvalidator.isPositive([]);\t\t// false\n```\n\n## Configuration\n\nCustomizing the validator.\n\n``` javascript\nvar validator = require('fluent-validator');\n\n// Adding custom validations\nvalidator.add('isEqualTo123', 'Value is not equal to 123', function(value) {\n\treturn value === 123;\n});\nvalidator.add('isDivisibleBy', 'Expected ${0} to be divisible by ${1}', function(value, divisibleBy) {\n\treturn value % divisibleBy === 0;\n});\n\n// Adding custom error thrower used in validation.throwOnError()\nvalidator.throwError = function(errors) {\n\tnew Error('Validation error. ' + errors.map(function(error) {\n\t\treturn error.message;\n\t}));\n});\n```\n\n## Validations\n\nList of available validations.\n\n### Comparisons\n\n- **isIn(value, arr)** - check if value is in array\n- **isPositive(value)** - check if `value \u003e 0`\n- **isNegative(value)** - check if `value \u003c 0`\n- **isNonNegative(value)** - check if `value \u003e= 0`\n- **isNonPositive(value)** - check if `value \u003c= 0`\n- **isLower(value, bound)** - check if `value \u003c bound`\n- **isLowerOrEql(value, bound)** - check if `value \u003c= bound`\n- **isGreater(value, bound)** - check if `value \u003e bound`\n- **isGreaterOrEql(value, bound)** - check if `value \u003e= bound`\n- **isInRange(value, min, max)** - check if `value \u003e min \u0026\u0026 value \u003c max`\n- **isInRangeOrEql(value, min, max)** - check if `value \u003e= min \u0026\u0026 value \u003c= max`\n\n### Dates\n\n- **isDate(value)** - check if value is of type `Date` or can be parsed with `Date.parse()`\n- **isAfter(value, min)** - check if `value \u003e min`\n- **isAfterOrEql(value, min)** - check if `value \u003e= min`\n- **isBefore(value, max)** - check if `value \u003c max`\n- **isBeforeOrEql(value, max)** - check if `value \u003c= max`\n\n### Numbers\n\n- **isInt(value)** - check if value is an numerical or textual representation of an integer\n- **isFloat(value)** - check if value is an numerical or textual representation of a float\n- **isNumber(value)** - check if value is an numerical or textual representation of a number\n- **isHexadecimal(value)** - check if value is an numerical or textual representation of a hexadecimal number\n- **isDivisibleBy(value, x)** - check if value is an numerical or textual representation of a number that is divisible by `x`\n\n### String\n\n- **contains(value, text)** (alias: isIn) - check if value is in text\n- **isLength(value, length)** - check if value is of given length\n- **matches(value, regexp)** - check if `regexp` matches `value`\n- **isAlpha(value)** - check if value is contains only `[a-zA-Z]`\n- **isNumeric(value)** - check if value is contains only `[0-9]`\n- **isAlphanumeric(value)** - check if value is contains only `[a-zA-Z0-9]`\n- **isLowercase(value)** - check if value is lowercased\n- **isUppercase(value)** - check if value is uppercased\n\n### Other\n\n- **isAscii(value)** - check if value is contains only ASCII characters\n- **isEmail(value)** - email RegExp validation\n- **isURL(value)** - URL RegExp validation\n- **isIP(value)** - IP RegExp validation\n- **isBase64(value)** - Base64 RegExp validation\n- **isHexColor(value)** - Base64 RegExp validation\n- **isUUID(value)** - UUID RegExp validation\n- **isJSON(value)** - UUID RegExp validation\n- **isCreditCard(value)** - CreditCard RegExp validation\n- **isISBN(value)** - ISBN RegExp validation\n- **isMongoObjectId(value)** - MongoObjectId RegExp validation\n- **isNull(value)** - checks if `value === null`\n- **isNotNull(value)** - checks if `value !== null`\n- **isUndefined(value)** - checks if `value === undefined`\n- **isNotUndefined(value)** - checks if `value !== undefined`\n- **isNullOrUndefined(value)** - checks if `value === undefined || value === null`\n- **isNotNullOrUndefined(value)** - checks if `value !== undefined \u0026\u0026 value !== null`\n- **isEmpty(value)** - checks if value is defined and is a non empty array or non empty object or non empty string\n- **isNotEmpty(value)** - negation of `isEmpty`\n\n### Custom validation\n\n- **passes(value, check, message)** - checks if `value` passes `check` function. In case of validation error `message` parameter is used.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoditorium%2Fnodejs-fluent-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoditorium%2Fnodejs-fluent-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoditorium%2Fnodejs-fluent-validator/lists"}