{"id":707,"date":"2019-03-01T14:17:16","date_gmt":"2019-03-01T06:17:16","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=707"},"modified":"2023-12-04T17:03:03","modified_gmt":"2023-12-04T09:03:03","slug":"joi%e9%aa%8c%e8%af%81","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=707","title":{"rendered":"Joi\u9a8c\u8bc1"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/hapijs\/joi\/blob\/v13.1.0\/API.md\">Joi\u5b98\u7f51<\/a><\/p>\n<h2>1\u3001\u5b9a\u4e49 schema \u65f6 Joi.object.keys() \u548c Joi.object() \u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/h2>\n<p>\u5e76\u65e0\u533a\u522b\uff0c\u5b98\u7f51\u7ed9\u4e86\u4e09\u79cd\u5b9a\u4e49\u5b9e\u73b0\u7684\u65b9\u5f0f\u3002\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">\/\/ \u4f7f\u7528 { } \u6765\u5b9a\u4e49\nconst schema = {\n    a: Joi.string(),\n    b: Joi.number()\n};\n\n\/\/ \u4f7f\u7528 Joi.object()\nconst schema = Joi.object({\n    a: Joi.string(),\n    b: Joi.number()\n});\n\n\/\/ \u4f7f\u7528 Joi.object.keys()\nconst schema = Joi.object().keys({\n    a: Joi.string(),\n    b: Joi.number()\n})\n<\/code><\/pre>\n<p>\u4e09\u79cd\u65b9\u5f0f\u5b9e\u73b0\u6548\u679c\u4e00\u6837\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u65f6\u4f1a\u7a0d\u6709\u4e0d\u540c\u3002<br \/>\n&#8211; \u5f53\u4f7f\u7528 {} \u65f6\uff0c\u53ea\u662f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u666e\u901a\u7684js\u5bf9\u8c61\uff0c\u5b83\u4e0d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684 schema \u5bf9\u8c61\u3002\u4f60\u53ef\u4ee5\u5c06\u5b83\u4f20\u9012\u7ed9\u9a8c\u8bc1\u65b9\u6cd5\uff0c\u4f46\u4e0d\u80fd\u8c03\u7528\u5bf9\u8c61\u7684validate\uff08\uff09\u65b9\u6cd5\uff0c\u5373\u7c7b\u4f3c\u8fd9\u79cd object.validate()\u7684\u64cd\u4f5c\u662f\u4e0d\u53ef\u4ee5\u7684\uff0c\u56e0\u4e3a\u5b83\u53ea\u662f\u4e00\u4e2a\u666e\u901a\u7684js\u5bf9\u8c61\u3002\u6b64\u5916\uff0c\u6bcf\u6b21\u5c06{}\u5bf9\u8c61\u4f20\u9012\u7ed9validate\uff08\uff09\u65b9\u6cd5\uff0c\u90fd\u5c06\u5bf9\u6bcf\u4e2a\u9a8c\u8bc1\u6267\u884c\u4e00\u4e2a\u6602\u8d35\u7684\u6a21\u5f0f\u7f16\u8bd1\u64cd\u4f5c\u3002<\/p>\n<ul>\n<li>\n<p>\u5f53\u4f7f\u7528 Joi.object() \u65f6\uff0c\u76f8\u5bf9\u4e8e\u4f7f\u7528 {} \uff0c\u8fd9\u662f\u6b63\u7ecf\u7684schema \u5bf9\u8c61\uff0c\u5b83\u4f1a\u5728\u7b2c\u4e00\u6b21\u7f16\u8bd1\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u591a\u6b21\u5c06\u5b83\u4f20\u9012\u7ed9validate\uff08\uff09\u65b9\u6cd5\uff0c\u4e0d\u4f1a\u589e\u52a0\u5f00\u9500\u3002\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u8bbe\u7f6e options \u6765\u9a8c\u8bc1\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u4f7f\u7528 Joi.object.keys() \u65f6\uff0c\u5176\u5b9e\u548c\u4f7f\u7528 Joi.object() \u662f\u7c7b\u4f3c\u7684\uff0c\u4f46\u662f\u5f53\u4f60\u60f3\u6dfb\u52a0\u66f4\u591a\u7684\u952e\uff08\u4f8b\u5982\u591a\u6b21\u8c03\u7528keys\uff08\uff09\uff09\u65f6\uff0c\u4f7f\u7528joi.object\uff08\uff09.keys\uff08[schema]\uff09\u4f1a\u66f4\u6709\u7528\u3002\u5982\u679c\u53ea\u6dfb\u52a0\u4e00\u7ec4\u952e\uff0c\u5219\u53ef\u4ee5\u8df3\u8fc7keys\uff08\uff09\u65b9\u6cd5\uff0c\u76f4\u63a5\u4f7f\u7528object\uff08\uff09\u3002\u6709\u4e9b\u4eba\u559c\u6b22\u7528keys\uff08\uff09\u6765\u4f7f\u4ee3\u7801\u770b\u8d77\u6765\u66f4\u52a0\u7cbe\u786e\uff08\u5176\u5b9e\u8fd9\u53ea\u662f\u4e00\u79cd\u7f16\u7a0b\u98ce\u683c\uff09\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>2. Joi.validate(value, schema, [options], [callback])\u4e2d\u7684 options \u53d6\u503c\u6709\u54ea\u4e9b\uff1f<\/h2>\n<ul>\n<li>abortEarly\uff1a \u8bbe\u7f6etrue\uff0c\u53ef\u4ee5\u5728\u68c0\u6d4b\u5230\u7b2c\u4e00\u4e2a\u9519\u8bef\u65f6\u7acb\u5373\u8fd4\u56de\uff0c\u9ed8\u8ba4false\uff08\u68c0\u67e5\u5168\u90e8\uff09\u3002\u63a8\u8350\u8bbe\u7f6etrue<\/li>\n<li>convert\uff1a\u8bbe\u7f6etrue\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u503c\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff09\u3002\u9ed8\u8ba4\u4e3atrue\u3002\u63a8\u8350\u91c7\u7528\u9ed8\u8ba4<\/li>\n<li>allowunknown\uff1a \u8bbe\u7f6etrue\uff0c\u5219\u5141\u8bb8\u5bf9\u8c61\u5305\u542b\u88ab\u5ffd\u7565\u7684\u672a\u77e5\u952e\u3002\u9ed8\u8ba4\u4e3afalse\u3002\u63a8\u8350\u8bbe\u7f6etrue<\/li>\n<li>skipfunctions\uff1a\u5982\u679c\u4e3atrue\uff0c\u5219\u5ffd\u7565\u5177\u6709\u51fd\u6570\u503c\u7684\u672a\u77e5\u952e\u3002\u9ed8\u8ba4\u4e3afalse\u3002\u63a8\u8350\u91c7\u7528\u9ed8\u8ba4<\/li>\n<li>stripunknown\uff1a \u5982\u679c\u4e3atrue,\u4ece\u5bf9\u8c61\u548c\u6570\u7ec4\u4e2d\u5220\u9664\u672a\u77e5\u7684\u5143\u7d20\u3002\u9ed8\u8ba4\u4e3afalse\u3002\u4e5f\u53ef\u4ee5\u7279\u6b8a\u7684\u8bbe\u7f6e\u6210 { objects: true , arrays: true }\u7684\u5f62\u5f0f\uff0c\u53ef\u4ee5\u5bf9\u8c61\u548c\u6570\u7ec4\u5206\u522b\u5904\u7406\u3002\u63a8\u8350\u91c7\u7528\u9ed8\u8ba4<\/li>\n<li>presence\uff1a \u8bbe\u7f6e\u9ed8\u8ba4\u7684\u53ef\u9009\u9700\u6c42\u3002\u652f\u6301\u7684\u6a21\u5f0f\uff1a\u2019optional\u2019,\u2019required\u2019,\u548c\u2019forbidden\u2019\u3002\u9ed8\u8ba4\u4e3a\u2019optional\u2019\u3002\u63a8\u8350\u91c7\u7528\u9ed8\u8ba4<\/li>\n<li>escapehtml\uff1a \u5f53\u4e3atrue\u65f6\uff0c\u51fa\u4e8e\u5b89\u5168\u76ee\u7684\uff0c\u9519\u8bef\u6d88\u606f\u6a21\u677f\u5c06\u7279\u6b8a\u5b57\u7b26\u8f6c\u4e49\u4e3ahtml\u5b9e\u4f53\u3002\u9ed8\u8ba4\u4e3afalse\u3002\u63a8\u8350\u91c7\u7528\u9ed8\u8ba4<\/li>\n<li>nodefaults\uff1a\u5982\u679c\u4e3atrue\uff0c\u5219\u4e0d\u5e94\u7528\u9ed8\u8ba4\u503c\u3002\u9ed8\u8ba4\u4e3afalse\u3002\u63a8\u8350\u91c7\u7528\u9ed8\u8ba4<\/li>\n<li>context\uff1a \u63d0\u4f9b\u4e00\u4e2a\u5916\u90e8\u6570\u636e\u96c6\u7528\u4e8e\u5f15\u7528\u3002\u53ea\u80fd\u8bbe\u7f6e\u4e3a\u5916\u90e8\u9009\u9879\u6765\u9a8c\u8bc1\uff08\uff09\u800c\u4e0d\u4f7f\u7528any.options\uff08\uff09\u3002\u4f7f\u7528\u65b9\u6cd5\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">const schema = Joi.object().keys({\n    a: Joi.ref('b.c'),\n    b: {\n        c: Joi.any()\n    },\n    c: Joi.ref('$x')\n});\n\nJoi.validate({ a: 5, b: { c: 5 } }, schema, { context: { x: 5 } }, (err, value) =&gt; {});\n<\/code><\/pre>\n<h2>3\u3001\u4e00\u4e9b\u4f8b\u5b50<\/h2>\n<pre><code class=\"language-javascript line-numbers\">let testData = { xxx };\n\nlet paramSchema = Joi.object().keys({\n    username: Joi.string().alphanum().min(3).max(30).required(), \/\/alphanum() \u4ec5\u5305\u542ba-z\uff0cA-Z\u548c0-9\u3002 string().min() \u5b57\u7b26\u4e32\u6700\u5c0f\u957f\u5ea6\n    password: Joi.string().regex(\/^[a-zA-Z0-9]{3,30}$\/),\n    access_token: [Joi.string(), Joi.number()], \/\/\u5b57\u7b26\u4e32\u6216\u6570\u5b57\n    birthyear: Joi.number().integer().min(1900).max(2013), \/\/integer() \u6574\u6570 number().min() \u6700\u5c0f\u503c\n    email: Joi.string().email(), \/\/string().emial() \u5b57\u7b26\u4e32\u503c\u65f6\u6709\u6548\u7535\u5b50\u90ae\u4ef6\u5730\u5740\n    website: Joi.string().uri({\n        scheme: [\n            'git',\n            \/git\\+https?\/\n        ]\n    }),\/\/\n    search: Joi.string().allow(''),\/\/\u5141\u8bb8\u7a7a\u503c\n    type: Joi.string().valid('disabled', 'normal', 'all').default('all'), \/\/valid() \u5141\u8bb8\u7684\u503c\n    startTime: Joi.date().min('1-1-1974').max('now'), \/\/\u65e5\u671f\n    endTime: Joi.when( Joi.ref('startTime'), { is: Joi.date().required(), then: Joi.date().max('1-1-2100') } ), \/\/Joi.ref()\u5f15\u7528 when(a,{is: , then: , otherwise: })\n    page: Joi.number().integer().min(1).default(1),\n    pageSize: Joi.number().integer().default(8),\n    deleteWhenLtTen: Joi.number().integer().max(10).strip(),\n    arraySelect: Joi.array().items(Joi.string().label('My string').required(), Joi.number().required()), \/\/\u6570\u7ec4schema\n});\n\nlet { error, value } = Joi.validate(testData, paramSchema, { allowUnknown: true, abortEarly: true });\nif (error) {\n    throw error;\n}\nquery = value;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Joi\u5b98\u7f51 1\u3001\u5b9a\u4e49 schema \u65f6 Joi.object.keys() \u548c Joi.object() \u6709\u4ec0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[414],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-15","tag-node"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=707"}],"version-history":[{"count":1,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":708,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/707\/revisions\/708"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}