{"name":"b-validate","version":"1.5.2","description":"Javascript type validate","main":"./dist/b-validate.cjs.js","module":"./es/index.js","types":"./es","scripts":{"prepublishOnly":"npm run test && 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"},"gitHead":"d4b69a6d49e89ac8dca6dc413f9a3ebf68135afb","_id":"b-validate@1.5.2","_nodeVersion":"16.13.1","_npmVersion":"8.1.2","dist":{"integrity":"sha512-zhIdocs1x9fJYBMc5Qe2kS3iJv8oRA55S3vV9ucdX+//ySV17PVuGGtwl6lwbbNvdjUt3Pt6ujL0GaVsXJy2ZA==","shasum":"b610b57df2fb90867357c321a32d50f7750f69ec","tarball":"http://registry-npm.ywwl.com/b-validate/download/b-validate-1.5.2.tgz","fileCount":34,"unpackedSize":96727,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIExsJ7lftlGzMnXqufG54ZJ2k5H1aSt2Mnk443KTeB1qAiEAgPU70Xim77XYPrdFClhxraNvA+jP/adO9fSMOC236i8="}],"size":13307,"noattachment":false,"key":"/b-validate/-/b-validate-1.5.2.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.2_1685091765637_0.9254180184619893"},"_hasShrinkwrap":false,"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":1685091765822,"_cnpm_publish_time":1685091765822,"dist-tags":{"beta":"1.5.0-beta.0","latest":"1.5.3"}}