{"id":13686728,"url":"https://github.com/fibjs-modules/enforce","last_synced_at":"2025-05-01T09:32:30.349Z","repository":{"id":73530139,"uuid":"174804711","full_name":"fibjs-modules/enforce","owner":"fibjs-modules","description":null,"archived":false,"fork":false,"pushed_at":"2020-09-04T15:55:22.000Z","size":61,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-12T09:48:57.531Z","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":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fibjs-modules.png","metadata":{"files":{"readme":"README.md","changelog":"History.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":"2019-03-10T09:51:16.000Z","updated_at":"2020-09-04T15:55:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"9a795621-c026-499b-8f0e-e5d126ee6171","html_url":"https://github.com/fibjs-modules/enforce","commit_stats":{"total_commits":29,"total_committers":1,"mean_commits":29.0,"dds":0.0,"last_synced_commit":"2067ed8509ddc539127bc3a99765f46f5e7ed39d"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs-modules%2Fenforce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs-modules%2Fenforce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs-modules%2Fenforce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs-modules%2Fenforce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fibjs-modules","download_url":"https://codeload.github.com/fibjs-modules/enforce/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251852896,"owners_count":21654481,"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-02T15:00:38.881Z","updated_at":"2025-05-01T09:32:30.342Z","avatar_url":"https://github.com/fibjs-modules.png","language":"JavaScript","funding_links":[],"categories":["Packages"],"sub_categories":["Validator"],"readme":"# @fibjs/enforce\n\nthis library is almost copied from [node-enforce](https://github.com/dresende/node-enforce) (even the document below.)\n\n## Data Validations\n\n[![Build Status](https://secure.travis-ci.org/fibjs-modules/enforce.png?branch=master)](https://travis-ci.org/fibjs-modules/enforce)\n[![Build status](https://ci.appveyor.com/api/projects/status/38egey3758071m4k?svg=true)](https://ci.appveyor.com/project/richardo2016/enforce)\n[![NPM version](https://img.shields.io/npm/v/@fibjs/enforce.svg)](https://www.npmjs.org/package/@fibjs/enforce)\n\n## Enforce\n\nYou can create a list of validations for several properties of an `Object` and then run the checks to\nsee if everything is OK.\n\n```js\nvar enforce = require(\"@fibjs/enforce\");\nvar checks  = new enforce.Enforce();\n\nchecks\n\t.add(\"name\", enforce.notEmptyString())\n\t.add(\"age\", enforce.ranges.number(18, undefined, \"under-age\"));\n\nchecks.check({\n\tname : \"John Doe\",\n\tage  : 16\n}, function (err) {\n\t// err should have an error with \"msg\" = \"under-age\"\n});\n```\n\nYou can pass some options in the constructor. One of them is `returnAllErrors` which makes the validations\nbe all checked before returning errors. With this option, if any error is found, even if it's only one, it will be\nreturned in an `Array`.\n\n```js\nvar enforce = require(\"@fibjs/enforce\");\nvar checks  = new enforce.Enforce({\n\treturnAllErrors : true\n});\n\nchecks\n\t.add(\"name\", enforce.notEmptyString())\n\t.add(\"name\", enforce.ranges.length(2)) // yes, you can have multiple validators per property\n\t.add(\"age\", enforce.ranges.number(18, undefined, \"under-age\"));\n\nchecks.check({\n\tname : \"J\",\n\tage  : 16\n}, function (err) {\n\tconsole.log(err);\n\t// [ { [Error: \"out-of-range-length\"], property: \"name\", value: \"J\" },\n\t//   { [Error: \"under-age\"], property: \"age\", value: 16 }]\n});\n```\n\n## Validators\n\nIn fact, all **enforcements** led to return one **Validator**, see detail of **Validator** [here](./src/enforcements/common.ts).\n\nAll validators accept a `msg` argument at the end. These argument is the error message returned if the\nvalidation fails. All validators return a `Validator` object that is used by `Enforce` to support chaining\nand other functionality. `Validator` objects have a `validate` method which is called by `Enforce` with the \nvalue of the property in question, a `next` callback and an optional global context table which may be used to\npass information between validators.\n\n### Required\n\n`enforce.required([ msg ])`\n\nChecks if a property is not `null` and not `undefined`. If can be `false`, `0` or `\"\"`.\n\n### Empty string\n\n`enforce.notEmptyString([ msg ])`\n\nChecks if a property length is not zero. It can actually work with `Array`s.\n\n### Same as\n\n`enforce.sameAs(property, [ msg ])`\n\nChecks if a property has the same (strict) value as another one. This is usefull for testing password matching.\n\n### Lists\n\n#### Inside a list\n\n`enforce.lists.inside(list[, msg ])`\n\nChecks if the property is inside a list of items.\n\n#### Outside a list\n\n`enforce.lists.outside(list[, msg ])`\n\nChecks if the property is not inside a list of items.\n\n### Ranges\n\n#### In a number range\n\n`enforce.ranges.number(min[, max[, msg ]])`\n\nChecks if a value is inside a specific range of numbers. Either `min` or `max` can be set to `undefined` meaning\nthat range side is `Infinity`.\n\nPlease note that this does not check for the type of value passed, you can even use this with `Date` objects.\n\n#### In a length range\n\n`enforce.ranges.length(min[, max[, msg ]])`\n\nDoes the same as the above but for the `length` property.\n\n### Security\n\n#### Username\n\n`enforce.security.username([[ opts, ]msg ])`\n\nChecks if a value matches a username format. `opts` is also optional and is an object with the following keys:\n\n- `length`: the minimal length of the username (default = 2)\n- `expr`: the regular expression to be used to match a valid username (if not passed, it's built based on the minimal length)\n\n#### Password\n\n`enforce.security.password([[ checks, ]msg ])`\n\nChecks if a value has some types of characters and a minimal length. `checks` has a default string `luns6` which means:\n\n- `l`: lowercase letters\n- `u`: uppercase letters\n- `n`: numbers\n- `s`: special characters\n- `6`: minimal length of 6\n\nYou can of course change this to \"lu4\" (lowercase, uppercase, minimal length of 4). Please note that if you pass only one argument\nto this validator, it will assume it's the `msg` argument. If you want to change the default checks, you have to pass both arguments.\n\n#### Credit Card\n\n`enforce.security.creditcard([[ types, ] msg ])`\n\nChecks if a value is a valid credit card number. It supports `amex` (American Express), `visa`, `maestro`, `discover` and `mastercard`.\nYou can change the list of supported cards (`types`) by passing a list with only some of them. You can also pass `luhn` which will ignore card\nprefixes and lenght and only pass the number using the Luhn algorithm.\n\n### Patterns\n\n#### Match\n\n`enforce.patterns.match(pattern, modifiers[, msg ])`\n\nChecks if property passes a specific regular expression. You can pass the `pattern` as a `RegExp` object (setting `modifiers` as `null`)\nor just pass a regular expression and it will be converted.\n\n#### Hex string\n\n`enforce.patterns.hexString([ msg ])`\n\nChecks if a property matches a predefined `RegExp` object accepting insensitive hexadecimal characters.\n\n#### E-mail\n\n`enforce.patterns.email([ msg ])`\n\nChecks if a property matches a predefined `RegExp` object accepting valid e-mail addresses.\n\n#### IPv4\n\n`enforce.patterns.ipv4([ msg ])`\n\nChecks if a property matches a predefined `RegExp` object accepting valid IPv4 address.\n\n#### IPv6\n\n`enforce.patterns.ipv6([ msg ])`\n\nChecks if a property matches a predefined `RegExp` object accepting valid IPv6 address.\n\n#### MAC\n\n`enforce.patterns.mac([ msg ])`\n\nChecks if a property matches a predefined `RegExp` object accepting valid MAC address.\n\n#### UUID v3\n\n`enforce.patterns.uuid3([ msg ])`\n\nChecks if a property matches a predefined `RegExp` object accepting valid UUID version 3.\n\n#### UUID v4\n\n`enforce.patterns.uuid4([ msg ])`\n\nChecks if a property matches a predefined `RegExp` object accepting valid UUID version 4.\n\n## Chaining\n`Enforce` supports chaining operations on all `Validator` objects, these allow you to add additional common\nconditions to each validation step. All chain operations return `Validator` objects, allowing you to chain\nmultiple commands together with ease.\n\n#### ifDefined\n\n`validation.ifDefined()`\n\nOnly proceedes to check the `validation` if the property's value is not `null` or `undefined`, passing validation\nif it is.\n\n#### ifNotEmptyString\n\n`validation.ifNotEmptyString()`\n\nOnly proceedes to check the `validation` if the property's value is a `string` with a length greater than 0.\n\n#### ifType\n\n`validation.ifType(type)`\n\nOnly proceedes to check the `validation` if the property's value is of the specified type. Checked with a `typeof value == type` operation.\n\n#### ifNotType\n\n`validation.ifNotType(type)`\n\nOnly proceedes to check the `validation` if the property's value is not of the specified type. Checked with a `typeof value != type` operation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibjs-modules%2Fenforce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffibjs-modules%2Fenforce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibjs-modules%2Fenforce/lists"}