1"use strict";(self.webpackChunkopa_website=self.webpackChunkopa_website||[]).push([[21227],{20134:(e,r,n)=>{n.r(r),n.d(r,{assets:()=>d,contentTitle:()=>i,default:()=>h,frontMatter:()=>a,metadata:()=>o,toc:()=>c});const o=JSON.parse('{"id":"errors/rego-parse-error/var-cannot-be-used-for-rule-name","title":"rego_parse_error: var cannot be used for rule name","description":"This cause for this error isn\'t always obvious at first glance, but as any parser error, this one is caused by an error","source":"@site/docs/errors/rego-parse-error/var-cannot-be-used-for-rule-name.md","sourceDirName":"errors/rego-parse-error","slug":"/errors/rego-parse-error/var-cannot-be-used-for-rule-name","permalink":"/docs/errors/rego-parse-error/var-cannot-be-used-for-rule-name","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{"sidebar_label":"var cannot be used for rule name","image":"/img/opa-errors.png"},"sidebar":"docsSidebar","previous":{"title":"unexpected string token","permalink":"/docs/errors/rego-parse-error/unexpected-string-token"},"next":{"title":"rule {name} is recursive","permalink":"/docs/errors/rego-recursion-error/rule-name-is-recursive"}}');var s=n(74848),t=n(28453);const a={sidebar_label:"var cannot be used for rule name",image:"/img/opa-errors.png"},i="rego_parse_error: var cannot be used for rule name",d={},c=[{value:"Examples",id:"examples",level:2},{value:"How To Fix It",id:"how-to-fix-it",level:2}];function l(e){const r={admonition:"admonition",code:"code",em:"em",h1:"h1",h2:"h2",header:"header",p:"p",pre:"pre",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",...(0,t.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(r.header,{children:(0,s.jsxs)(r.h1,{id:"rego_parse_error-var-cannot-be-used-for-rule-name",children:[(0,s.jsx)(r.code,{children:"rego_parse_error"}),": var cannot be used for rule name"]})}),"\n",(0,s.jsxs)(r.p,{children:["This cause for this error isn't always obvious at first glance, but as any parser error, this one is caused by an error\nin the syntax of a Rego policy, and one around ",(0,s.jsx)(r.em,{children:"rule declarations"})," specifically. Look closely at the rule head for\nanything that might be missing! The following examples will help demonstrate the most common causes for this error."]}),"\n",(0,s.jsxs)(r.table,{children:[(0,s.jsx)(r.thead,{children:(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.th,{children:"Stage"}),(0,s.jsx)(r.th,{children:"Category"}),(0,s.jsx)(r.th,{children:"Message"})]})}),(0,s.jsx)(r.tbody,{children:(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"parsing"})}),(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"rego_parse_error"})}),(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"var cannot be used for rule name"})})]})})]}),"\n",(0,s.jsx)(r.h2,{id:"examples",children:"Examples"}),"\n",(0,s.jsxs)(r.p,{children:["The below example shows the most likely occurrence of this error: having\nforgotten to import ",(0,s.jsx)(r.code,{children:"if"}),", either via ",(0,s.jsx)(r.code,{children:"import future.keywords.if"}),", or ",(0,s.jsx)(r.code,{children:"import rego.v1"})," (OPA v0.59.0 to v0.x, before v1 syntax became the default):"]}),"\n",(0,s.jsx)(r.admonition,{type:"note",children:(0,s.jsxs)(r.p,{children:["Note that in OPA versions ",(0,s.jsx)(r.code,{children:">=1.0.0"}),", imports for ",(0,s.jsx)(r.code,{children:"if"})," etc. are no longer\nrequired."]})}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-rego",children:'package policy\n\nallow if {\n input.request.method == "GET"\n input.request.path == ["users"]\n}\n'})}),"\n",(0,s.jsx)(r.p,{children:"Another example could be having forgotten to add an assignment operator between the rule name and the value intended\nfor assignment:"}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-rego",children:'package policy\n\nimport future.keywords.if\n\n# note the missing `:=` between internal_user and email\ninternal_user email if {\n endswith(email, "@acmecorp.com")\n\n email := input.user.email\n}\n'})}),"\n",(0,s.jsx)(r.h2,{id:"how-to-fix-it",children:"How To Fix It"}),"\n",(0,s.jsxs)(r.p,{children:["If caused by a missing import like the ",(0,s.jsx)(r.code,{children:"if"})," keyword, add the import at\nthe top of your package. Other cases are likely caused by having forgotten to\nadd an assignment operator between the rule name and the value to assign. Once\nyou have identified the cause, fix the syntax error or adding the missing import\nwill address the issue."]})]})}function h(e={}){const{wrapper:r}={...(0,t.R)(),...e.components};return r?(0,s.jsx)(r,{...e,children:(0,s.jsx)(l,{...e})}):l(e)}},28453:(e,r,n)=>{n.d(r,{R:()=>a,x:()=>i});var o=n(96540);const s={},t=o.createContext(s);function a(e){const r=o.useContext(t);return o.useMemo((function(){return"function"==typeof e?e(r):{...r,...e}}),[r,e])}function i(e){let r;return r=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:a(e.components),o.createElement(t.Provider,{value:r},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.