1"use strict";(self.webpackChunkopa_website=self.webpackChunkopa_website||[]).push([[34707],{28453:(e,n,r)=>{r.d(n,{R:()=>a,x:()=>o});var s=r(96540);const t={},i=s.createContext(t);function a(e){const n=s.useContext(i);return s.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function o(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(t):e.components||t:a(e.components),s.createElement(i.Provider,{value:n},e.children)}},83924:(e,n,r)=>{r.r(n),r.d(n,{assets:()=>l,contentTitle:()=>o,default:()=>d,frontMatter:()=>a,metadata:()=>s,toc:()=>u});const s=JSON.parse('{"id":"rules/custom/prefer-value-in-head","title":"prefer-value-in-head","description":"Summary: Prefer value in rule head","source":"@site/projects/regal/rules/custom/prefer-value-in-head.md","sourceDirName":"rules/custom","slug":"/rules/custom/prefer-value-in-head","permalink":"/projects/regal/rules/custom/prefer-value-in-head","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{},"sidebar":"autoSidebar","previous":{"title":"one-liner-rule","permalink":"/projects/regal/rules/custom/one-liner-rule"},"next":{"title":"Fixing Violations","permalink":"/projects/regal/fixing"}}');var t=r(74848),i=r(28453);const a={},o="prefer-value-in-head",l={},u=[{value:"Rationale",id:"rationale",level:2},{value:"Configuration Options",id:"configuration-options",level:2},{value:"Related Resources",id:"related-resources",level:2}];function c(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,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.header,{children:(0,t.jsx)(n.h1,{id:"prefer-value-in-head",children:"prefer-value-in-head"})}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Summary"}),": Prefer value in rule head"]}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Category"}),": Custom"]}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Avoid"})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rego",children:'package policy\n\npin_as_number := val if {\n is_number(input.pin_code)\n val := to_number(input.pin_code)\n}\n\ndeny contains message if {\n not input.user\n message := "user attribute missing from input"\n}\n'})}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Prefer"})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rego",children:'package policy\n\npin_as_number := to_number(input.pin_code) if is_number(input.pin_code)\n\ndeny contains "user attribute missing from input" if not input.user\n'})}),"\n",(0,t.jsx)(n.h2,{id:"rationale",children:"Rationale"}),"\n",(0,t.jsx)(n.p,{children:'Rules that return the value assigned in the last expression of the rule body may have the value, or the function\nreturning the value, moved directly to the rule head. This creates more succinct rules, and often allows for rules to be\nexpressed as "one-liners". This is not a general recommendation, but a style preference that a team or organization\nmight want to standardize on. As such, it is placed in the custom category, and must be explicitly enabled in\nconfiguration.'}),"\n",(0,t.jsxs)(n.p,{children:["The ",(0,t.jsx)(n.code,{children:"only-scalars"})," configuration option may be used to only suggest moving scalar values (strings, numbers, booleans,\nnull) to the head, and not expressions or functions returning a value. With this option set to ",(0,t.jsx)(n.code,{children:"true"}),", the following\nexample would be flagged:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rego",children:'deny contains message if {\n not input.user\n # value is a scalar\n message := "user attribute missing from input"\n}\n'})}),"\n",(0,t.jsx)(n.p,{children:"But not:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rego",children:'deny contains message if {\n not input.user\n # value returned from a function call, not suggested if `only-scalars` is set to `true`\n message := sprintf("user attribute missing from input: %v", [input])\n}\n'})}),"\n",(0,t.jsxs)(n.p,{children:["The ",(0,t.jsx)(n.code,{children:"include-interpolated"})," configuration option may be used to count interpolated strings as a scalar (string) values,\nwhich will have Regal recommend moving them to the head even when ",(0,t.jsx)(n.code,{children:"only-scalars"})," is set to ",(0,t.jsx)(n.code,{children:"true"}),"."]}),"\n",(0,t.jsx)(n.h2,{id:"configuration-options",children:"Configuration Options"}),"\n",(0,t.jsx)(n.p,{children:"This linter rule provides the following configuration options:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-yaml",children:'rules:\n custom:\n prefer-value-in-head:\n # note that all rules in the "custom" category are disabled by default\n #
1(i.e. level "ignore")\n #\n # one of "error", "warning", "ignore"\n level: error\n # whether to only suggest moving scalar values (strings, numbers, booleans, null)\n # to the head, and not expressions or functions\n only-scalars: false\n # when set to true, counts interpolated strings as a scalar value, and will suggest\n # moving them to the head even when `only-scalars` is true\n include-interpolated: false\n # variable names to exempt from the rule (by default, none)\n except-var-names:\n - report\n - violation\n'})}),"\n",(0,t.jsx)(n.h2,{id:"related-resources",children:"Related Resources"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:["GitHub: ",(0,t.jsx)(n.a,{href:"https://github.com/open-policy-agent/regal/blob/main/bundle/regal/rules/custom/prefer-value-in-head/prefer_value_in_head.rego",children:"Source Code"})]}),"\n"]})]})}function d(e={}){const{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(c,{...e})}):c(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.