Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Add validations for new RLS editor (#22227)

Joshen Lim8f950a111e1b
diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx
index 4770cec345c..281aa5e0706 100644
--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx
+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx
@@ -61,7 +61,7 @@ export const PolicyDetailsV2 = ({ isEditing, form, onUpdateCommand }: PolicyDeta
return (
<>
<div className="px-5 py-5 flex flex-col gap-y-4 border-b">
- <div className="flex items-center justify-between gap-4 grid grid-cols-12">
+ <div className="flex items-start justify-between gap-4 grid grid-cols-12">
<FormField_Shadcn_
control={form.control}
name="name"
diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
index 0f2d0c1419d..b89b0f0b9aa 100644
--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
@@ -95,6 +95,7 @@ export const AIPolicyEditorPanel = memo(function ({
// [Joshen] Hyrid form fields, just spit balling to get a decent POC out
const [using, setUsing] = useState('')
const [check, setCheck] = useState('')
+ const [fieldError, setFieldError] = useState<string>()
const [showCheckBlock, setShowCheckBlock] = useState(false)
const monacoOneRef = useRef<Monaco | null>(null)
@@ -126,7 +127,7 @@ export const AIPolicyEditorPanel = memo(function ({
const formId = 'rls-editor'
const FormSchema = z.object({
- name: z.string(),
+ name: z.string().min(1, 'Please provide a name'),
table: z.string(),
behavior: z.string(),
command: z.string(),
@@ -308,6 +309,14 @@ export const AIPolicyEditorPanel = memo(function ({
const using = editorOneRef.current?.getValue().trim() ?? undefined
const check = editorTwoRef.current?.getValue().trim() ?? undefined
+ if (command === 'insert' && (check === undefined || check.length === 0)) {
+ return setFieldError('Please provide a SQL expression for the WITH CHECK statement')
+ } else if (command !== 'insert' && (using === undefined || using.length === 0)) {
+ return setFieldError('Please provide a SQL expression for the USING statement')
+ } else {
+ setFieldError(undefined)
+ }
+
if (selectedPolicy === undefined) {
const sql = generateCreatePolicyQuery({
name: name,
@@ -372,6 +381,7 @@ export const AIPolicyEditorPanel = memo(function ({
setUsing('')
setCheck('')
setShowCheckBlock(false)
+ setFieldError(undefined)
form.reset(defaultValues)
} else {
@@ -511,6 +521,7 @@ export const AIPolicyEditorPanel = memo(function ({
isEditing={selectedPolicy !== undefined}
form={form}
onUpdateCommand={(command: string) => {
+ setFieldError(undefined)
if (!['update', 'all'].includes(command)) setShowCheckBlock(false)
}}
/>
@@ -615,13 +626,20 @@ export const AIPolicyEditorPanel = memo(function ({
/>
)}
+ {fieldError !== undefined && (
+ <p className="px-5 py-2 pb-0 text-sm text-red-900">{fieldError}</p>
+ )}
+
{supportWithCheck && (
<div className="px-5 py-3 flex items-center gap-x-2">
<Checkbox_Shadcn_
id="use-check"
name="use-check"
checked={showCheckBlock}
- onCheckedChange={() => setShowCheckBlock(!showCheckBlock)}
+ onCheckedChange={() => {
+ setFieldError(undefined)
+ setShowCheckBlock(!showCheckBlock)
+ }}
/>
<Label_Shadcn_ className="text-xs cursor-pointer" htmlFor="use-check">
Use check expression
@@ -709,6 +727,7 @@ export const AIPolicyEditorPanel = memo(function ({
setCheck(` ${value.check}`)
setExpOneLineCount(1)
setExpTwoLineCount(1)
+ setFieldError(undefined)
if (!['update', 'all'].includes(value.command.toLowerCase())) {
setShowCheckBlock(false)