PageSourceSearch

https://www.openpolicyagent.org/assets/js/b985d0a0.29902749.js

js openpolicyagent.org collected 2026-09-24 08:32:11 UTC 4,164 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkopa_website=self.webpackChunkopa_website||[]).push([[40090],{21480:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>a,contentTitle:()=>c,default:()=>d,frontMatter:()=>r,metadata:()=>s,toc:()=>l});const s=JSON.parse('{"id":"rules/idiomatic/custom-has-key-construct","title":"custom-has-key-construct","description":"Summary: Custom function may be replaced by in and object.keys","source":"@site/projects/regal/rules/idiomatic/custom-has-key-construct.md","sourceDirName":"rules/idiomatic","slug":"/rules/idiomatic/custom-has-key-construct","permalink":"/projects/regal/rules/idiomatic/custom-has-key-construct","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{},"sidebar":"autoSidebar","previous":{"title":"boolean-assignment","permalink":"/projects/regal/rules/idiomatic/boolean-assignment"},"next":{"title":"custom-in-construct","permalink":"/projects/regal/rules/idiomatic/custom-in-construct"}}');var o=t(74848),i=t(28453);const r={},c="custom-has-key-construct",a={},l=[{value:"Rationale",id:"rationale",level:2},{value:"Configuration Options",id:"configuration-options",level:2},{value:"Related Resources",id:"related-resources",level:2}];function u(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",header:"header",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,i.R)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)(n.header,{children:(0,o.jsx)(n.h1,{id:"custom-has-key-construct",children:"custom-has-key-construct"})}),"\n",(0,o.jsxs)(n.p,{children:[(0,o.jsx)(n.strong,{children:"Summary"}),": Custom function may be replaced by ",(0,o.jsx)(n.code,{children:"in"})," and ",(0,o.jsx)(n.code,{children:"object.keys"})]}),"\n",(0,o.jsxs)(n.p,{children:[(0,o.jsx)(n.strong,{children:"Category"}),": Idiomatic"]}),"\n",(0,o.jsx)(n.p,{children:(0,o.jsx)(n.strong,{children:"Avoid"})}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-rego",children:'package policy\n\nmfa if has_key(input.claims, "mfa")\n\nhas_key(map, key) if {\n    _ = map[key]\n}\n'})}),"\n",(0,o.jsx)(n.p,{children:(0,o.jsx)(n.strong,{children:"Prefer"})}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-rego",children:'package policy\n\nmfa if "mfa" in object.keys(input.claims)\n'})}),"\n",(0,o.jsx)(n.h2,{id:"rationale",children:"Rationale"}),"\n",(0,o.jsxs)(n.p,{children:["Checking if a key exists in an object (regardless of the attribute's value) used to be done using custom functions. With\nthe introduction of the ",(0,o.jsx)(n.a,{href:"https://www.openpolicyagent.org/docs/policy-reference/#builtin-object-objectkeys",children:"object.keys"}),"\n(OPA ",(0,o.jsx)(n.a,{href:"https://github.com/open-policy-agent/opa/releases/tag/v0.47.0",children:"v0.47.0"}),") function, this is no longer necessary,\nand using the built-in function together with ",(0,o.jsx)(n.code,{children:"in"})," should be preferred."]}),"\n",(0,o.jsx)(n.h2,{id:"configuration-options",children:"Configuration Options"}),"\n",(0,o.jsx)(n.p,{children:"This linter rule provides the following configuration options:"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-yaml",children:'rules:\n  idiomatic:\n    custom-has-key-construct:\n      # one of "error", "warning", "ignore"\n      level: error\n'})}),"\n",(0,o.jsx)(n.h2,{id:"related-resources",children:"Related Resources"}),"\n",(0,o.jsxs)(n.ul,{children:["\n",(0,o.jsxs)(n.li,{children:["GitHub: ",(0,o.jsx)(n.a,{href:"https://github.com/open-policy-agent/regal/blob/main/bundle/regal/rules/idiomatic/custom-has-key-construct/custom_has_key_construct.rego",children:"Source Code"})]}),"\n"]})]})}function d(e={}){const{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,o.jsx)(n,{...e,children:(0,o.jsx)(u,{...e})}):u(e)}},28453:(e,n,t)=>{t.d(n,{R:()=>r,x:()=>c});var s=t(96540);const o={},i=s.createContext(o);function r(e){const n=s.useContext(i);return s.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function c(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:r(e.components),s.createElement(i.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.