mirror of
https://github.com/usebruno/bruno.git
synced 2025-06-21 04:08:01 +02:00
feat(#682): Add validation for request vars & dynamic vars
This commit is contained in:
parent
3a5a213242
commit
827df18c62
@ -23,7 +23,7 @@ const EnvironmentVariables = ({ environment, collection }) => {
|
|||||||
enabled: Yup.boolean(),
|
enabled: Yup.boolean(),
|
||||||
name: Yup.string()
|
name: Yup.string()
|
||||||
.required('Name cannot be empty')
|
.required('Name cannot be empty')
|
||||||
.matches(/^[^\d]\w*$/, 'Name contains invalid characters')
|
.matches(/^(?!\d)\w*$/, 'Name contains invalid characters')
|
||||||
.trim(),
|
.trim(),
|
||||||
secret: Yup.boolean(),
|
secret: Yup.boolean(),
|
||||||
type: Yup.string(),
|
type: Yup.string(),
|
||||||
|
@ -8,6 +8,7 @@ import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collection
|
|||||||
import SingleLineEditor from 'components/SingleLineEditor';
|
import SingleLineEditor from 'components/SingleLineEditor';
|
||||||
import Tooltip from 'components/Tooltip';
|
import Tooltip from 'components/Tooltip';
|
||||||
import StyledWrapper from './StyledWrapper';
|
import StyledWrapper from './StyledWrapper';
|
||||||
|
import toast from 'react-hot-toast';
|
||||||
|
|
||||||
const VarsTable = ({ item, collection, vars, varType }) => {
|
const VarsTable = ({ item, collection, vars, varType }) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
@ -29,7 +30,19 @@ const VarsTable = ({ item, collection, vars, varType }) => {
|
|||||||
const _var = cloneDeep(v);
|
const _var = cloneDeep(v);
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'name': {
|
case 'name': {
|
||||||
_var.name = e.target.value;
|
const value = e.target.value;
|
||||||
|
|
||||||
|
if (/^(?!\d).*$/.test(value) === false) {
|
||||||
|
toast.error('Variable names must not start with a number!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (/^\w*$/.test(value) === false) {
|
||||||
|
toast.error('Variable contains invalid character! Variables must only contain alpha-numeric characters.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_var.name = value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'value': {
|
case 'value': {
|
||||||
@ -88,7 +101,7 @@ const VarsTable = ({ item, collection, vars, varType }) => {
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{vars && vars.length
|
{vars && vars.length
|
||||||
? vars.map((_var, index) => {
|
? vars.map((_var) => {
|
||||||
return (
|
return (
|
||||||
<tr key={_var.uid}>
|
<tr key={_var.uid}>
|
||||||
<td>
|
<td>
|
||||||
|
@ -1,42 +1,12 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import get from 'lodash/get';
|
import get from 'lodash/get';
|
||||||
import { useDispatch } from 'react-redux';
|
|
||||||
import { updateRequestScript, updateResponseScript } from 'providers/ReduxStore/slices/collections';
|
|
||||||
import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions';
|
|
||||||
import { useTheme } from 'providers/Theme';
|
|
||||||
import VarsTable from './VarsTable';
|
import VarsTable from './VarsTable';
|
||||||
import StyledWrapper from './StyledWrapper';
|
import StyledWrapper from './StyledWrapper';
|
||||||
|
|
||||||
const Vars = ({ item, collection }) => {
|
const Vars = ({ item, collection }) => {
|
||||||
const dispatch = useDispatch();
|
|
||||||
const requestVars = item.draft ? get(item, 'draft.request.vars.req') : get(item, 'request.vars.req');
|
const requestVars = item.draft ? get(item, 'draft.request.vars.req') : get(item, 'request.vars.req');
|
||||||
const responseVars = item.draft ? get(item, 'draft.request.vars.res') : get(item, 'request.vars.res');
|
const responseVars = item.draft ? get(item, 'draft.request.vars.res') : get(item, 'request.vars.res');
|
||||||
|
|
||||||
const { storedTheme } = useTheme();
|
|
||||||
|
|
||||||
const onRequestScriptEdit = (value) => {
|
|
||||||
dispatch(
|
|
||||||
updateRequestScript({
|
|
||||||
script: value,
|
|
||||||
itemUid: item.uid,
|
|
||||||
collectionUid: collection.uid
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const onResponseScriptEdit = (value) => {
|
|
||||||
dispatch(
|
|
||||||
updateResponseScript({
|
|
||||||
script: value,
|
|
||||||
itemUid: item.uid,
|
|
||||||
collectionUid: collection.uid
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const onRun = () => dispatch(sendRequest(item, collection.uid));
|
|
||||||
const onSave = () => dispatch(saveRequest(item.uid, collection.uid));
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StyledWrapper className="w-full flex flex-col">
|
<StyledWrapper className="w-full flex flex-col">
|
||||||
<div className="flex-1 mt-2">
|
<div className="flex-1 mt-2">
|
||||||
|
@ -59,10 +59,24 @@ class Bru {
|
|||||||
throw new Error('Key is required');
|
throw new Error('Key is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (/^(?!\d)\w*$/.test(key) === false) {
|
||||||
|
throw new Error(
|
||||||
|
`Variable name: "${key}" contains invalid characters!` +
|
||||||
|
' Names must only contain alpha-numeric characters and cannot start with a digit.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this.collectionVariables[key] = value;
|
this.collectionVariables[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
getVar(key) {
|
getVar(key) {
|
||||||
|
if (/^(?!\d)\w*$/.test(key) === false) {
|
||||||
|
throw new Error(
|
||||||
|
`Variable name: "${key}" contains invalid characters!` +
|
||||||
|
' Names must only contain alpha-numeric characters and cannot start with a digit.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return this.collectionVariables[key];
|
return this.collectionVariables[key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user