{"_id":"@cgfamily/think-validator","name":"@cgfamily/think-validator","dist-tags":{"latest":"1.6.7"},"versions":{"1.6.7":{"name":"@cgfamily/think-validator","version":"1.6.7","description":"Validator for ThinkJS","main":"src/index.js","scripts":{"lint":"eslint --fix rules.js & eslint --fix src/index.js","test":"npm run lint && nyc ava test/index.js","coverage":"npm test && nyc report --reporter=html"},"ava":{"files":["test/*.js"]},"repository":{"type":"git","url":"git+https://github.com/thinkjs/think-validator.git"},"keywords":["think-validator"],"author":{"name":"lushijie","email":"lushijie1218@126.com"},"contributors":[{"name":"lushijie","email":"lushijie1218@126.com"}],"license":"ISC","bugs":{"url":"https://github.com/thinkjs/think-validator/issues"},"homepage":"https://github.com/thinkjs/think-validator#readme","devDependencies":{"ava":"~0.18.1","babel-eslint":"~7.1.1","coveralls":"~2.11.16","eslint":"~4.2.0","eslint-config-think":"~1.0.1","nyc":"~10.1.2"},"dependencies":{"think-helper":"^1.1.3","validator":"^13.7.0"},"gitHead":"846bd2caec7cb06c41fd05bf5f8082e90e928eaf","_id":"@cgfamily/think-validator@1.6.7","_nodeVersion":"14.18.2","_npmVersion":"6.14.15","dist":{"integrity":"sha512-/bvp+oueK0JjRbIlGblGY0NGrM80Js9c68tqbAvKsu0+h0n6Z6ClsF2jwHeStwXSfJsQ/rh+cZq8gTZVvjx5+w==","shasum":"65fc09469511f3406ddb7fb22ad3a7c8c700d21f","tarball":"https://registry.npmjs.org/@cgfamily/think-validator/-/think-validator-1.6.7.tgz","fileCount":11,"unpackedSize":125961,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIB7gL819iFt/CHs1b/PCnSSAaGKIyOnX9KqveP9jyfOZAiEAp/m3OS3AloTQMcGPI8MSpj5mOfWnu12V7owKl9EciQ0="}]},"_npmUser":{"name":"cgfamily","email":"18751290129@163.com"},"directories":{},"maintainers":[{"name":"cgfamily","email":"18751290129@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/think-validator_1.6.7_1705397351394_0.25158617373619263"},"_hasShrinkwrap":false}},"time":{"created":"2024-01-16T09:29:11.324Z","1.6.7":"2024-01-16T09:29:11.564Z","modified":"2024-01-16T09:29:11.851Z"},"maintainers":[{"name":"cgfamily","email":"18751290129@163.com"}],"description":"Validator for ThinkJS","homepage":"https://github.com/thinkjs/think-validator#readme","keywords":["think-validator"],"repository":{"type":"git","url":"git+https://github.com/thinkjs/think-validator.git"},"contributors":[{"name":"lushijie","email":"lushijie1218@126.com"}],"author":{"name":"lushijie","email":"lushijie1218@126.com"},"bugs":{"url":"https://github.com/thinkjs/think-validator/issues"},"license":"ISC","readme":"# think-validator\r\n[![Build Status](https://travis-ci.org/thinkjs/think-validator.svg?branch=master)](https://travis-ci.org/thinkjs/think-validator)\r\n[![Coverage Status](https://coveralls.io/repos/github/thinkjs/think-validator/badge.svg?branch=master)](https://coveralls.io/github/thinkjs/think-validator?branch=master)\r\n[![npm](https://img.shields.io/npm/v/think-validator.svg?style=flat-square)](https://www.npmjs.com/package/think-validator)\r\n\r\n- [think-validator](#think-validator)\r\n    + [How to Use in Thinkjs3.0](#how-to-use-in-thinkjs30)\r\n    + [Validation Rules Config](#validation-rules-config)\r\n    + [Basic Data Type](#basic-data-type)\r\n    + [Data Type Auto Convert Before Validation](#data-type-auto-convert-before-validation)\r\n    + [Data Type Auto Convert After Validation](#data-type-auto-convert-after-validation)\r\n    + [Nested Validation](#nested-validation)\r\n      - [Nested Validation for Array](#nested-validation-for-array)\r\n      - [Nested Validation for Object](#nested-validation-for-object)\r\n    + [Alias for Param Name](#alias-for-param-name)\r\n    + [Custom Error Message](#custom-error-message)\r\n      - [For Not Object Type](#for-not-object-type)\r\n      - [For Object Type](#for-object-type)\r\n    + [Add Custom Valid Method](#add-custom-valid-method)\r\n    + [Supported Validation Type](#supported-validation-type)\r\n      - [requiredIf: [Array]](#requiredif--array)\r\n      - [requiredNotIf: [Array]](#requirednotif--array)\r\n      - [requiredWith: [Array]](#requiredwith--array)\r\n      - [requiredWithAll: [Array]](#requiredwithall--array)\r\n      - [requiredWithOut: [Array]](#requiredwithout--array)\r\n      - [requiredWithOutAll: [Array]](#requiredwithoutall--array)\r\n      - [contains: [String]](#contains--string)\r\n      - [equals: [String]](#equals--string)\r\n      - [different: [String]](#different--string)\r\n      - [before: [true|date format string]](#before--truedate-format-string)\r\n      - [after: [true|date format string]](#after--truedate-format-string)\r\n      - [alpha: [true]](#alpha--true)\r\n      - [alphaDash: [true]](#alphadash--true)\r\n      - [alphaNumeric: [true]](#alphanumeric--true)\r\n      - [alphaNumericDash: [true]](#alphanumericdash--true)\r\n      - [ascii: [true]](#ascii--true)\r\n      - [base64: [true]](#base64--true)\r\n      - [byteLength: [{min: 0, max: 10}]](#bytelength--min-0-max-10)\r\n      - [creditCard: [true]](#creditcard--true)\r\n      - [currency: [true|options]](#currency--trueoptions)\r\n      - [date: [true]](#date--true)\r\n      - [decimal: [true]](#decimal--true)\r\n      - [divisibleBy: [number]](#divisibleby--number)\r\n      - [email: [true|options]](#email--trueoptions)\r\n      - [fqdn: [true|options]](#fqdn--trueoptions)\r\n      - [float: [true|{min: 0, max: 10}]](#float--truemin-0-max-10)\r\n      - [fullWidth: [true]](#fullwidth--true)\r\n      - [halfWidth: [true]](#halfwidth--true)\r\n      - [hexColor: [true]](#hexcolor--true)\r\n      - [hex: [true]](#hex--true)\r\n      - [ip: [true]](#ip--true)\r\n      - [ip4: [true]](#ip4--true)\r\n      - [ip6: [true]](#ip6--true)\r\n      - [isbn: [true]](#isbn--true)\r\n      - [isin: [true]](#isin--true)\r\n      - [iso8601: [true]](#iso8601--true)\r\n      - [in: [Array]](#in--array)\r\n      - [notIn: [Array]](#notin--array)\r\n      - [int: [true|{min: 0, max: 10}]](#int--truemin-0-max-10)\r\n      - [length: [{min: 0, max: 10}]](#length--min-0-max-10)\r\n      - [lowercase: [true]](#lowercase--true)\r\n      - [uppercase: [true]](#uppercase--true)\r\n      - [mobile: [true|locale]](#mobile--truelocale)\r\n      - [mongoId: [true]](#mongoid--true)\r\n      - [multibyte: [true]](#multibyte--true)\r\n      - [url: [true|options]](#url--trueoptions)\r\n      - [field: [true]](#field--true)\r\n      - [field: [true]](#field--true-1)\r\n      - [image: [true]](#image--true)\r\n      - [startWith: [String]](#startwith--string)\r\n      - [endWith: [String]](#endwith--string)\r\n      - [string: [true]](#string--true)\r\n      - [array: [true]](#array--true)\r\n      - [boolean: [true]](#boolean--true)\r\n      - [object: [true]](#object--true)\r\n      - [regexp: [Regexp]](#regexp--regexp)\r\n      - [issn: [true]](#issn--true)\r\n      - [uuid: [true]](#uuid--true)\r\n      - [md5: [true]](#md5--true)\r\n      - [macAddress: [true]](#macaddress--true)\r\n      - [dataURI: [true]](#datauri--true)\r\n      - [variableWidth: [true]](#variablewidth--true)\r\n\r\n\r\n### How to Use in Thinkjs3.0\r\n\r\n\r\n```js\r\n// In your logic dir, xxx.js\r\nlet ret = this.validate(rules, msgs)\r\n```\r\n\r\n* `rules`: the validation rules.\r\n* `msgs`: the custom error messages.\r\n* If valid ok, the `ret` is `true`, else `ret` is `false`. When valid failed, you can get the error message like {param1: 'error message', ...} in `this.validateErrors`, .\r\n\r\n### Validation Rules Config\r\n\r\nValidation rules is written in json like this:\r\n\r\n```js\r\nlet rules = {\r\n  id: {\r\n    int: true,\r\n    required: true,\r\n    trim: true,\r\n    default: 12,\r\n    method: 'GET,POST'\r\n  }\r\n}\r\n```\r\n\r\n* Param is not `required` by default, so if you need param not empty, you should assign `required` with `true`.\r\n* If you want `trim` the space for the param you should assign `trim` with `true`,for example, if the id's value is '12   ' and `trim: true` then `id` is an integer, but it won't been an integer with `trim: false`.\r\n* With `default` you can give the param default value, if param's value is true empty, it will be the default value.\r\n* By default `method` eq ctx.method, only when `method` include the ctx.method validation will be run.\r\n\r\n### Basic Data Type\r\n\r\n* The supported data types include boolean,string,int,float,array,object. And the default type is string. Only one basic data type is permit at the same time in one rule.\r\n\r\n### Data Type Auto Convert Before Validation\r\n\r\n* When valid'type is `boolean`, `['yes', 'on', '1', 'true', true]` will auto convert into `true`, and others to `false`.\r\n* When valid'type is `array` and param's value is not array: if param's value is string, it will run `split(,)`, else it will convert param's value to `[param's value]`.\r\n\r\n### Data Type Auto Convert After Validation\r\n\r\n* When valid'type is `int` or `float`, if pass the validation the param's value will auto convert into integer.\r\n\r\n\r\n### Nested Validation\r\n\r\nNested validation will valid the item in array or object. But it only support one layer child validation and every child'rule should be the same for now.\r\n\r\n\r\n#### Nested Validation for Array\r\n\r\n```js\r\nlet rules = {\r\n  array: true,\r\n  children: {\r\n    int: true,\r\n    trim: true,\r\n    required: true\r\n  }\r\n}\r\n```\r\n\r\n#### Nested Validation for Object\r\n\r\n```js\r\nlet rules = {\r\n  object: true,\r\n  children: {\r\n    int: true,\r\n    trim: true,\r\n    required: true\r\n  }\r\n}\r\n```\r\n\r\n\r\n### Alias for Param Name\r\n\r\nFor example,\r\n\r\n```js\r\nlet rules = {\r\n  user: {\r\n    required: true\r\n  }\r\n}\r\nthis.validate(rules);\r\n```\r\n\r\nIf valid failed, the default error would be, {user: 'user can not be blank'}, but sometime you may want 'user' to be '用户名', you can add 'aliasName' for the rule. Just like:\r\n\r\n```js\r\nlet rules = {\r\n  user: {\r\n    required: true,\r\n    aliasName: '用户名'\r\n  }\r\n}\r\n\r\nthis.validate(rules);\r\n```\r\n\r\nAnd the error would be {user: '用户名 can not be blank'}.\r\n\r\nIf you want use `aliasName` for array or object, please add `aliasName` in children:\r\n\r\n```js\r\n// for array\r\nlet rules = {\r\n  user: {\r\n    array: true,\r\n    children: {\r\n      aliasName: '用户名'\r\n    }\r\n  }\r\n}\r\n\r\nthis.validate(rules);\r\n```\r\n\r\n\r\n```js\r\n// for object\r\nlet rules = {\r\n  user: {\r\n    object: true,\r\n    children: {\r\n      aliasName: '用户名'\r\n    }\r\n  }\r\n}\r\n\r\nthis.validate(rules);\r\n```\r\n\r\n### Custom Error Message\r\n\r\n#### For Not Object Type\r\n\r\n```js\r\nlet rules = {\r\n  username: {\r\n    required: true,\r\n    method: 'GET'\r\n  }\r\n}\r\nlet msgs = {\r\n  required: '{name} can not blank',         // rule 1\r\n  username: '{name} can not blank',         // rule 2\r\n  username: {\r\n    required: '{name} can not blank'        // rule 3\r\n  }\r\n}\r\n```\r\n\r\nIt will find the matched error message when valid failed by the order: rule3> rule2 > rule1.\r\n\r\n#### For Object Type\r\n```js\r\n  let rules = {\r\n    address: {\r\n      object: true,\r\n      children: {\r\n        int: true\r\n      }\r\n    }\r\n  }\r\n  let msgs = {\r\n    int: 'this is int error message for all field',             // rule 1\r\n    address: {\r\n      int: 'this is int error message for address',             // rule 2\r\n      a: 'this is int error message for a of address',          // rule 3\r\n      'b,c': 'this is int error message for b and c of address' // rule 4\r\n      d: {\r\n        int: 'this is int error message for d of address'       // rule 5\r\n      }\r\n    }\r\n  }\r\n  let flag = this.validate(rules, msgs);\r\n```\r\n\r\nIt will find the matched error message when valid failed by the order: rule5 > rule4 rule3> rule2 > rule1.\r\n\r\n### Add Custom Valid Method\r\n\r\n* You can parse the rule's arguments with query before validation.\r\n*  Just add a _ruleMethodName function for the ruleMethodName.\r\n\r\n* If ctx.method == GET, currentQuery eq the get query param of ctx,\r\nif ctx.method == POST| PUT | DELETE | PATCH | LINK | UNLINK, currentQuery eq the post query param of ctx.\r\nif ctx.method == FILE, currentQuery eq the file query param of ctx.\r\n\r\n\r\n```js\r\n// in src/config/validator.js\r\nmodule.exports = {\r\n  rules: {\r\n    /**\r\n     * @param  {Mixed} validValue  [the origin rule's value]\r\n     * @param  {Object}      [the ctx query which match the ctx.method]\r\n     * @return {Mixed}             [the rule's value after parse]\r\n     */\r\n    _newrule: function(validValue, { rule, ctx, validName, currentQuery, rules }) {\r\n      return validValue;\r\n    },\r\n    /**\r\n     * @param  {Mixed} value            [the argument'value need to valid]\r\n     * @param  {Mixed}  [the rule's value after parse]\r\n     * @return {Boolean}                []\r\n     */\r\n    newrule: function(value, { rule, validName, validValue, parsedValidValue, ctx, currentQuery, rules }) {\r\n      return value === validValue;\r\n    }\r\n  },\r\n  messages: {\r\n    newrule: 'this is newrule custom message'\r\n  }\r\n}\r\n```\r\n\r\n\r\n### Supported Validation Type\r\n\r\n####  requiredIf:  [Array]\r\nIf the `requiredIf`'s argument's first item has value in request data, let the first item is the value(in request data).\r\nIf the `requiredIf`'s argument's first item does not have value in request data, let the first item keep intact.\r\nIf the first item is in the last items, the param's value is required.\r\n\r\n####  requiredNotIf:  [Array]\r\nIf the `requiredNotIf`'s argument's first item has value in request data, let the first item is the value(in request data).\r\nIf the `requiredNotIf`'s argument's first item does not have value in request data, let the first item keep intact.\r\nIf the first item is not in the last items, the param's value is required.\r\n\r\n####  requiredWith:  [Array]\r\nWhen some items of `requiredWith`'s arugument is not true empty in request data, the param's value is required.\r\n\r\n####  requiredWithAll:  [Array]\r\nWhen all items of `requiredWithAll`'s arugument is not true empty in request data, the param's value is required.\r\n\r\n####  requiredWithOut:  [Array]\r\nWhen some items of `requiredWithOut`'s arugument is true empty in request data, the param's value is required.\r\n\r\n####  requiredWithOutAll:  [Array]\r\nWhen all items of `requiredWithOutAll`'s arugument is true empty in request data, the param's value is required.\r\n\r\n####  contains:  [String]\r\nIf the `contains`'s argument does not have value in request data, the rule will check if param's value contains `equals`'s argument.\r\n\r\n####  equals:  [String]\r\nIf the `equals`'s argument has value in request data, the rule will check if the value(in request data) equal param's value.\r\n\r\n####  different:  [String]\r\nIf the `equals`'s argument has value in request data, the rule will check if the value(in request data) not equal param's value.\r\nIf the `equals`'s argument does not have value in request data, the rule will check if `equals`'s argument not equal param's value.\r\n\r\n####  before:  [true|date format string]\r\nCheck if param's value before the giving date.\r\nIf `before` = true, the giving date is `now`.\r\n\r\n####  after:  [true|date format string]\r\nCheck if param's value after the giving date.\r\nIf `after` = true, the giving date is `now`.\r\n\r\n####  alpha:  [true]\r\nCheck if param's value contains only letters (a-zA-Z).\r\n\r\n####  alphaDash:  [true]\r\nCheck if param's value contains only letters (a-zA-Z_).\r\n\r\n####  alphaNumeric:  [true]\r\nCheck if param's value contains only letters, numbers.\r\n\r\n####  alphaNumericDash:  [true]\r\nCheck if param's value contains only letters, numbers and _.\r\n\r\n####  ascii:  [true]\r\nCheck if param's value is ascii.\r\n\r\n####  base64:  [true]\r\nCheck if param's value is base64.\r\n\r\n####  byteLength:  [{min: 0, max: 10}] | 10\r\nCheck if param's value length(in bytes) falls in a range.\r\n\r\n####  creditCard:  [true]\r\nCheck if param's value is creditCard.\r\n\r\n####  currency:  [true|options]\r\nCheck if param's value is currency format.\r\n`options` please see [validator.js](https://github.com/chriso/validator.js).\r\n\r\n####  date:  [true]\r\nCheck if param's value is date format.\r\n\r\n####  decimal:  [true]\r\nCheck if param's value represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.\r\n\r\n####  divisibleBy:  [number]\r\nCheck if param's value is a number that's divisible by the giving one.\r\n\r\n####  email:  [true|options]\r\nCheck if param's value is an email.\r\n`options` please see [validator.js](https://github.com/chriso/validator.js).\r\n\r\n####  fqdn:  [true|options]\r\nCheck if param's value is fqdn.\r\n`options` please see [validator.js](https://github.com/chriso/validator.js).\r\n\r\n####  float:  [true|{min: 0, max: 10}]\r\nIf `float` = true, check if param's value is a float.\r\nIf `float` = {min: 0, max: 10}, check if param's value is a float between `min` and 'max'.\r\n\r\n####  fullWidth:  [true]\r\nCheck if param's value contains any full-width chars.\r\n\r\n####  halfWidth:  [true]\r\nCheck if param's value contains any half-width chars.\r\n\r\n####  hexColor:  [true]\r\nCheck if param's value is a hexadecimal color.\r\n\r\n####  hex:  [true]\r\nCheck if param's value is a hexadecimal number.\r\n\r\n####  ip:  [true]\r\nCheck if param's value is an ip4 or ip6.\r\n\r\n####  ip4:  [true]\r\nCheck if param's value is an ip4.\r\n\r\n####  ip6:  [true]\r\nCheck if param's value is an ip6.\r\n\r\n####  isbn:  [true]\r\nCheck if param's value is an isbn.\r\n\r\n####  isin:  [true]\r\nCheck if param's value is an ISIN (stock/security identifier).\r\n\r\n####  iso8601:  [true]\r\nCheck if param's value is a valid ISO 8601 date.\r\n\r\n####  in:  [Array]\r\nCheck if param's value is in a array of allowed values.\r\n\r\n####  notIn:  [Array]\r\nCheck if param's value is not in a array of allowed values.\r\n\r\n####  int:  [true|{min: 0, max: 10}]\r\nIf `int` = true, check if param's value is an integer.\r\nIf `int` = {min: 0, max: 10}, check if param's value is an integer between `min` and 'max'.\r\n\r\n####  length:  [{min: 0, max: 10}] | 10\r\nCheck if param's value length falls in a range.\r\n\r\n####  lowercase:  [true]\r\nCheck if param's value is lowercase.\r\n\r\n####  uppercase:  [true]\r\nCheck if param's value is uppercase.\r\n\r\n####  mobile:  [true|locale]\r\nCheck if param's value is a mobile phone number.\r\n`locale` please see [validator.js](https://github.com/chriso/validator.js).\r\n\r\n####  mongoId:  [true]\r\nCheck if param's value is a valid hex-encoded representation of a MongoDB ObjectId.\r\n\r\n####  multibyte:  [true]\r\nCheck if param's value contains one or more multibyte chars.\r\n\r\n####  url:  [true|options]\r\nCheck if param's value is an URL.\r\n`options` please see [validator.js](https://github.com/chriso/validator.js).\r\n\r\n####  field:  [true]\r\nCheck if param's value is a sql order string.\r\n\r\n####  field:  [true]\r\nCheck if param's value is a sql field string.\r\n\r\n####  image:  [true]\r\nCheck if param's value is an image file.\r\n\r\n####  startWith:  [String]\r\nCheck if param's value start with the giving string.\r\n\r\n####  endWith:  [String]\r\nCheck if param's value end with the giving string.\r\n\r\n####  string:  [true]\r\nCheck if param's value is string.\r\n\r\n####  array:  [true]\r\nCheck if param's value is array.\r\nIf param's value is not array, it will convert to `[param's value]`.\r\n\r\n####  boolean:  [true]\r\nCheck if param's value is boolean.\r\nIf param's value is one of ['yes', 'on', '1', 'true', true], it will convert to `true`, and others will convert to `false`.\r\n\r\n####  object:  [true]\r\nCheck if param's value is object.\r\n\r\n####  regexp:  [Regexp]\r\nCheck if param's value match the regexp.\r\n\r\n####  issn:  [true]\r\nCheck if param's value is an ISSN.\r\n\r\n####  uuid:  [true]\r\nCheck if param's value is a UUID (version 3, 4 or 5).\r\n\r\n####  md5:  [true]\r\nCheck if param's value is md5.\r\n\r\n####  macAddress:  [true]\r\nCheck if param's value is macaddress.\r\n\r\n####  dataURI:  [true]\r\nCheck if param's value is a data uri format.\r\n\r\n####  variableWidth:  [true]\r\nCheck if param's value contains a mixture of full and half-width chars.\r\n","readmeFilename":"README.md"}