PageSourceSearch

https://www.openpolicyagent.org/assets/js/57687f84.80429f51.js

js openpolicyagent.org collected 2026-09-24 08:28:33 UTC 3,920 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkopa_website=self.webpackChunkopa_website||[]).push([[9174],{28453:(e,n,o)=>{o.d(n,{R:()=>i,x:()=>c});var t=o(96540);const s={},r=t.createContext(s);function i(e){const n=t.useContext(r);return t.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(s):e.components||s:i(e.components),t.createElement(r.Provider,{value:n},e.children)}},29107:(e,n,o)=>{o.r(n),o.d(n,{assets:()=>l,contentTitle:()=>c,default:()=>d,frontMatter:()=>i,metadata:()=>t,toc:()=>a});const t=JSON.parse('{"id":"rules/idiomatic/use-object-keys","title":"use-object-keys","description":"Summary: Prefer to use object.keys","source":"@site/projects/regal/rules/idiomatic/use-object-keys.md","sourceDirName":"rules/idiomatic","slug":"/rules/idiomatic/use-object-keys","permalink":"/projects/regal/rules/idiomatic/use-object-keys","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{},"sidebar":"autoSidebar","previous":{"title":"use-in-operator","permalink":"/projects/regal/rules/idiomatic/use-in-operator"},"next":{"title":"use-object-union-n","permalink":"/projects/regal/rules/idiomatic/use-object-union-n"}}');var s=o(74848),r=o(28453);const i={},c="use-object-keys",l={},a=[{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,r.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.header,{children:(0,s.jsx)(n.h1,{id:"use-object-keys",children:"use-object-keys"})}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"Summary"}),": Prefer to use ",(0,s.jsx)(n.code,{children:"object.keys"})]}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"Category"}),": Idiomatic"]}),"\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.strong,{children:"Avoid"})}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rego",children:"package policy\n\nkeys := {k | some k, _ in input.object}\n\n# or\n\nkeys := {k | some k; input.object[k]}\n"})}),"\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.strong,{children:"Prefer"})}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rego",children:"package policy\n\nkeys := object.keys(input.object)\n"})}),"\n",(0,s.jsx)(n.h2,{id:"rationale",children:"Rationale"}),"\n",(0,s.jsxs)(n.p,{children:["Instead of using a set comprehension to collect keys from an object, prefer to use the built-in function\n",(0,s.jsx)(n.a,{href:"https://www.openpolicyagent.org/docs/policy-reference/#builtin-object-objectkeys",children:"object.keys"}),".\nThis option is both more declarative and better conveys the intent of the code."]}),"\n",(0,s.jsx)(n.h2,{id:"configuration-options",children:"Configuration Options"}),"\n",(0,s.jsx)(n.p,{children:"This linter rule provides the following configuration options:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-yaml",children:'rules:\n  idiomatic:\n    use-object-keys:\n      # one of "error", "warning", "ignore"\n      level: error\n'})}),"\n",(0,s.jsx)(n.h2,{id:"related-resources",children:"Related Resources"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["OPA Docs: ",(0,s.jsx)(n.a,{href:"https://www.openpolicyagent.org/docs/policy-reference/#builtin-object-objectkeys",children:"object.keys"})]}),"\n",(0,s.jsxs)(n.li,{children:["GitHub: ",(0,s.jsx)(n.a,{href:"https://github.com/open-policy-agent/regal/blob/main/bundle/regal/rules/idiomatic/use-object-keys/use_object_keys.rego",children:"Source Code"})]}),"\n"]})]})}function d(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(u,{...e})}):u(e)}}}]);

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.