{"name":"b-validate","version":"1.5.1","description":"Javascript type validate","main":"./dist/b-validate.cjs.js","module":"./es/index.js","types":"./es","scripts":{"prepublishOnly":"npm run build","dev-cjs":"rollup -c -f cjs -o ./dist/b-validate.cjs.js -w","dev-es":"rollup -c -w","build:es":"rm -rf es/* && BUILD_TYPE=es rollup -c","build:dist":"rm -rf dist/*  && BUILD_TYPE=dist rollup -c ","build":"npm run build:es & npm run build:dist ","test":"npm run build:es && jest"},"repository":{"type":"git","url":"git+https://github.com/PengJiyuan/b-validate.git"},"keywords":["validate","type"],"author":{"name":"PengJiyuan"},"license":"MIT","bugs":{"url":"https://github.com/PengJiyuan/b-validate/issues"},"homepage":"https://github.com/PengJiyuan/b-validate#readme","devDependencies":{"@babel/preset-env":"^7.3.4","@rollup/plugin-typescript":"^8.3.1","babel-jest":"^24.5.0","jest":"^24.5.0","rollup":"^3.23.0","@rollup/plugin-buble":"*","@rollup/plugin-commonjs":"*","@rollup/plugin-node-resolve":"*","tslib":"^2.3.1","typescript":"^4.6.3"},"licenseText":"MIT License\n\nCopyright (c) 2019 PengJiyuan\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","_id":"b-validate@1.5.1","dist":{"shasum":"c45ae7b5f58accbfb5be9d1fc983fe43c29873c8","integrity":"sha512-9fxaXSj424TdDolLq/XU0cgLtl9GvYaFmbx1S2DaVBQtqvQyg73pp2/jFBbFMpfx5TweBo9gN7fk0f/te8qPxw==","tarball":"http://registry-npm.ywwl.com/b-validate/download/b-validate-1.5.1.tgz","fileCount":9,"unpackedSize":57673,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCjgerT0gxg9m2CQXMhsM20W/ypAokU9R4eCUAjCucgkgIgMGSgO/ww9f7A5X2PMGxfvla7Tdwz17kd6MnbvmjbMA8="}],"size":8489,"noattachment":false,"key":"/b-validate/-/b-validate-1.5.1.tgz"},"_npmUser":{"name":"yinkaihui","email":"yinkaihui0828@163.com"},"directories":{},"maintainers":[{"name":"pjy","email":"731401082@qq.com"},{"name":"yinkaihui","email":"yinkaihui0828@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/b-validate_1.5.1_1685091008415_0.058107702690081364"},"_hasShrinkwrap":false,"deprecated":"wrong format","readme":"[![Travis](https://img.shields.io/travis/PengJiyuan/b-validate.svg)](https://travis-ci.org/PengJiyuan/b-validate)\n[![npm](https://img.shields.io/npm/v/b-validate.svg)](https://www.npmjs.com/package/b-validate)\n[![npm](https://img.shields.io/npm/l/b-validate.svg)](https://www.npmjs.com/package/b-validate)\n[![codecov.io](https://codecov.io/github/PengJiyuan/b-validate/coverage.svg?branch=master)](https://codecov.io/github/PengJiyuan/b-validate?branch=master)\n\n# b-validate\n\nJavascript type validate.\n\n## Highlight\n\n- chainable api\n- support custom validator\n- support schema validate\n- support async validate\n\n## Usage\n\n```bash\nnpm i b-validate\n```\n\n```js\nimport bv from 'b-validate';\n\nbv(123)\n  .number.min(2)\n  .max(10)\n  .collect((error) => {\n    console.log(error); // { value: 123, type: 'number', message: '123 is not less than 10' }\n    // if no error, error equal to null\n  });\n\n// or get error message like this:\n\nconst error = bv('b-validate').string.isRequired.match(/Validator/).end;\n// { value: 'b-validate', type: 'string', message: '`b-validate` is not match pattern /Validator/' }\n```\n\n### Schema\n\n```js\nimport { Schema } from 'b-validate';\n\nconst schema = new Schema({\n  name: [\n    {\n      type: 'string',\n      required: true,\n      message: '必填字段',\n    },\n    {\n      type: 'string',\n      maxLength: 10,\n      message: '最大长度是10',\n    },\n  ],\n  age: [\n    {\n      type: 'number',\n      min: 2,\n      max: 5,\n      message: '在2和5之间',\n    },\n  ],\n  email: [\n    {\n      type: 'email',\n      message: '邮箱格式不对',\n    },\n  ],\n  ip: [\n    {\n      type: 'ip',\n      message: 'ip格式不对',\n    },\n  ],\n  url: [\n    {\n      type: 'url',\n      message: 'url格式不对',\n    },\n  ],\n  custom: [\n    {\n      validator: (value, callback) => {\n        if (value > 10) {\n          callback('不能大于10！');\n        }\n      },\n    },\n  ],\n  // Async validate\n  async: [\n    {\n      validator: async (value, callback) => {\n        if (value > 10) {\n          callback('不能大于10！');\n        }\n      },\n    },\n  ],\n});\n\nschema.validate(\n  {\n    name: 'pengjiyuan is a nice boy',\n    age: 24,\n    email: 'pengjiyuan@bytedance.com',\n    ip: '127.0.0.1',\n    url: 'https://bytedancecom',\n    custom: 20,\n    async: 20,\n  },\n  (errors) => {\n    console.log(errors);\n    /*\n     * {\n     *  value: 'pengjiyuan is a nice boy', type: 'string', message: '最大长度是10' },\n     *  age: { value: 24, type: 'number', message: '在2和5之间' },\n     *  url: { value: 'https://bytedancecom', type: 'url', message: 'url格式不对' },\n     *  custom: { value: 20, type: 'custom', message: '不能大于10！' }\n     * }\n     */\n  }\n);\n```\n\n### Validate Messages\n\nCustomize the validate messages by `options.validateMessages` or `(new schema({})).messages` / `bv.setGlobalConfig({ validateMessages: {} })`.\n\n```\nimport bv from 'b-validate';\n\n// set validateMessages for specific validator\nconst error = bv('', {validateMessages: {required: '必须有值'}}).string.isRequired.end.message;\n// output: 必须有值\n\n```\n\n```\nimport bv from 'b-validate';\n\n// set global validatemessages\nbv.setGlobalConfig({ validateMessages: {required: '必须有值'} });\n\nconst error = bv('').string.isRequired.end.message;\n// output: 必须有值\n```\n\n```\nimport bv from 'b-validate';\n\nconst error = bv('', {validateMessages: {required: '必须有值'}}).string.isRequired.end.message;\n// output: 必须有值\n```\n\n```\nimport { Schema } from 'b-validate';\nimport zhCN from 'b-validate/es/locale/zh-CN';\n\nconst schema = new Schema({\n  name: [{ type: 'string', length: 4 }],\n});\n\nschema.messages(zhCN);\n\nschema.validate({name: 'aaa'}, errors => {\n  console.log(errors);\n  // {name: { message: '字符数必须是 4' }}\n})\n```\n\n## Api\n\n### String\n\n#### .maxLength(length: number)\n\nValidate string's max length.\n\n#### .minLength(length: number)\n\nValidate string's min length.\n\n#### .length(length: number)\n\nValidate string's length\n\n#### .match(pattern: regexPattern)\n\nValidate whether string is match regex pattern.\n\n#### .uppercase\n\nValidate whether string is uppercase.\n\n#### .lowercase\n\nValidate whether string is lowercase.\n\n#### Demo\n\n```js\nbv('vvvv').string.isRequired.minLength(2).maxLength(10).lowercase.end; // pass\n```\n\n### Number\n\n#### .min(num: number)\n\nMinimum value.\n\n#### .max(num: number)\n\nMaximum value.\n\n#### .equal(num: number)\n\nEqual to number.\n\n#### .range(min: number, max: number)\n\nIn range min ~ max.\n\n#### .positive\n\nIs it a positive number?\n\n#### .negative\n\nIs it a negative number?\n\n#### Demo\n\n```js\nbv(123).number.min(2).max(250).positive.end; // pass\n```\n\n### Array\n\n#### .length(length: number)\n\nValidate array's length\n\n#### .includes(arrays: any[])\n\nWhether array includes some array.\n\n#### .deepEqual(other: any[])\n\nDeep equal to other array\n\n#### Demo\n\n```js\nbv([1, 2, 3]).array.length(3).includes([1, 2]).deepEqual([1, 2, 3]).end; // pass\n```\n\n### Object\n\n#### .hasKeys(keys: string[])\n\nObject has some keys?\n\n#### .deepEqual(obj: object)\n\nDeep equal to other object.\n\n#### .empty\n\nEmpty object.\n\n#### Demo\n\n```js\nbv({ a: 1, b: 2 }).object.hasKeys(['a', 'b']).deepEqual({ a: 1, b: 2 }).not.empty.end; // pass\n```\n\n### Type\n\n#### .email\n\n```js\nbv('abc@qq.com').type.email.end; // pass\n```\n\n#### .url\n\n```js\nbv('https://baidu.com').type.url.end; // pass\n```\n\n#### .ip\n\n```js\nbv('127.0.0.1').type.ip.end; // pass\n```\n\n### Custom validator\n\n```js\nbv('xxxxxx').custom.validate(\n  (value, callback) => {\n    if (value !== 'x') {\n      callback(`Expect x but got ${value}`);\n    }\n  },\n  (error) => {\n    console.log(error); // { value: 'xxxxxx', type: 'string', message: 'Expect x but got xxxxxx' }\n  }\n);\n```\n\n### Async validate\n\n```js\nbv('xxxxxx').custom.validate(\n  async (value, callback) => {\n    const apiValue = api.getValue();\n    if (value !== apiValue) {\n      callback(`Expect x but got ${apiValue}`);\n    }\n  },\n  (error) => {\n    console.log(error);\n  }\n);\n```\n\n## LICENSE\n\n[MIT](./LICENSE) © [PengJiyuan](https://github.com/PengJiyuan)\n","publish_time":1685091008543,"_cnpm_publish_time":1685091008543,"dist-tags":{"beta":"1.5.0-beta.0","latest":"1.5.3"}}