Skip to content

Commit 807ee4d

Browse files
committed
Policy Field: Add MaxLen, Required Priority and Fix Decimal issues
1 parent 0239de6 commit 807ee4d

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

workspaces/dcm/plugins/dcm/src/pages/policies/components/PolicyFormFields.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ export function PolicyFormFields({
7171

7272
<TextField
7373
label="Description"
74-
helperText="Optional — describe the purpose of this policy"
74+
helperText={
75+
err('description') ?? 'Optional — describe the purpose of this policy'
76+
}
77+
error={Boolean(err('description'))}
7578
value={form.description}
7679
onChange={e =>
7780
setForm(prev => ({ ...prev, description: e.target.value }))
7881
}
82+
onBlur={() => touch('description')}
7983
fullWidth
8084
variant="outlined"
8185
size="small"
@@ -111,19 +115,28 @@ export function PolicyFormFields({
111115
</FormControl>
112116

113117
<TextField
114-
label="Priority"
118+
label="Priority *"
115119
helperText={
116120
err('priority') ?? '1 (highest) – 1000 (lowest), default 500'
117121
}
118122
error={Boolean(err('priority'))}
119123
value={form.priority}
120-
onChange={e => setForm(prev => ({ ...prev, priority: e.target.value }))}
124+
onChange={e => {
125+
if (/^\d*$/.test(e.target.value)) {
126+
setForm(prev => ({ ...prev, priority: e.target.value }));
127+
}
128+
}}
129+
onKeyDown={e => {
130+
if (e.key === '.' || e.key === 'e' || e.key === 'E') {
131+
e.preventDefault();
132+
}
133+
}}
121134
onBlur={() => touch('priority')}
122135
fullWidth
123136
variant="outlined"
124137
size="small"
125138
type="number"
126-
inputProps={{ min: 1, max: 1000 }}
139+
inputProps={{ min: 1, max: 1000, step: 1 }}
127140
/>
128141

129142
<TextField

workspaces/dcm/plugins/dcm/src/pages/policies/policyFormTypes.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ describe('validatePolicyForm', () => {
5252

5353
const ok = validatePolicyForm({ ...valid(), priority: '500' });
5454
expect(ok.priority).toBeUndefined();
55+
56+
const decimal = validatePolicyForm({ ...valid(), priority: '500.5' });
57+
expect(decimal.priority).toBeDefined();
58+
59+
const empty = validatePolicyForm({ ...valid(), priority: '' });
60+
expect(empty.priority).toBeDefined();
61+
});
62+
63+
it('rejects description longer than 255 characters', () => {
64+
const errors = validatePolicyForm({
65+
...valid(),
66+
description: 'a'.repeat(256),
67+
});
68+
expect(errors.description).toBeDefined();
69+
70+
const ok = validatePolicyForm({ ...valid(), description: 'a'.repeat(255) });
71+
expect(ok.description).toBeUndefined();
5572
});
5673

5774
it('accepts only GLOBAL or USER as policy_type', () => {

workspaces/dcm/plugins/dcm/src/pages/policies/policyFormTypes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ const policySchema = yup.object({
3737
.required('Display name is required')
3838
.min(1, 'Display name cannot be empty')
3939
.max(255, 'Display name must be at most 255 characters'),
40+
description: yup
41+
.string()
42+
.max(255, 'Description must be at most 255 characters'),
4043
policy_type: yup
4144
.string()
4245
.required('Policy type is required')
4346
.oneOf(['GLOBAL', 'USER'], 'Must be GLOBAL or USER'),
4447
priority: yup
4548
.number()
4649
.typeError('Priority must be a number')
50+
.required('Priority is required')
51+
.integer('Priority must be a whole number')
4752
.min(1, 'Priority must be at least 1')
4853
.max(1000, 'Priority must be at most 1000'),
4954
rego_code: yup

0 commit comments

Comments
 (0)