forked from extern/bruno
fix(#853): Allow - in variable names & small refactor
This commit is contained in:
parent
b633fc58d2
commit
44d90c42ed
@ -10,6 +10,7 @@ import StyledWrapper from './StyledWrapper';
|
|||||||
import { useFormik } from 'formik';
|
import { useFormik } from 'formik';
|
||||||
import * as Yup from 'yup';
|
import * as Yup from 'yup';
|
||||||
import { uuid } from 'utils/common';
|
import { uuid } from 'utils/common';
|
||||||
|
import { envVariableNameRegex } from 'utils/common/regex';
|
||||||
|
|
||||||
const EnvironmentVariables = ({ environment, collection }) => {
|
const EnvironmentVariables = ({ environment, collection }) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
@ -23,7 +24,10 @@ 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(
|
||||||
|
envVariableNameRegex,
|
||||||
|
'Name contains invalid characters. Must only contain alphanumeric characters, "-" and "_"'
|
||||||
|
)
|
||||||
.trim(),
|
.trim(),
|
||||||
secret: Yup.boolean(),
|
secret: Yup.boolean(),
|
||||||
type: Yup.string(),
|
type: Yup.string(),
|
||||||
|
@ -9,6 +9,7 @@ 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';
|
import toast from 'react-hot-toast';
|
||||||
|
import { envVariableNameRegex } from 'utils/common/regex';
|
||||||
|
|
||||||
const VarsTable = ({ item, collection, vars, varType }) => {
|
const VarsTable = ({ item, collection, vars, varType }) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
@ -37,8 +38,10 @@ const VarsTable = ({ item, collection, vars, varType }) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (/^\w*$/.test(value) === false) {
|
if (envVariableNameRegex.test(value) === false) {
|
||||||
toast.error('Variable contains invalid character! Variables must only contain alpha-numeric characters.');
|
toast.error(
|
||||||
|
'Variable contains invalid character! Variables must only contain alpha-numeric characters, "-" and "_".'
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
packages/bruno-app/src/utils/common/regex.js
Normal file
1
packages/bruno-app/src/utils/common/regex.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const envVariableNameRegex = /^(?!\d)[\w-]*$/;
|
@ -1,6 +1,8 @@
|
|||||||
const Handlebars = require('handlebars');
|
const Handlebars = require('handlebars');
|
||||||
const { cloneDeep } = require('lodash');
|
const { cloneDeep } = require('lodash');
|
||||||
|
|
||||||
|
const envVariableNameRegex = /^(?!\d)[\w-]*$/;
|
||||||
|
|
||||||
class Bru {
|
class Bru {
|
||||||
constructor(envVariables, collectionVariables, processEnvVars, collectionPath) {
|
constructor(envVariables, collectionVariables, processEnvVars, collectionPath) {
|
||||||
this.envVariables = envVariables;
|
this.envVariables = envVariables;
|
||||||
@ -59,10 +61,10 @@ class Bru {
|
|||||||
throw new Error('Key is required');
|
throw new Error('Key is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (/^(?!\d)\w*$/.test(key) === false) {
|
if (envVariableNameRegex.test(key) === false) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Variable name: "${key}" contains invalid characters!` +
|
`Variable name: "${key}" contains invalid characters!` +
|
||||||
' Names must only contain alpha-numeric characters and cannot start with a digit.'
|
' Names must only contain alpha-numeric characters, "-", "_" and cannot start with a digit.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +72,7 @@ class Bru {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getVar(key) {
|
getVar(key) {
|
||||||
if (/^(?!\d)\w*$/.test(key) === false) {
|
if (envVariableNameRegex.test(key) === false) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Variable name: "${key}" contains invalid characters!` +
|
`Variable name: "${key}" contains invalid characters!` +
|
||||||
' Names must only contain alpha-numeric characters and cannot start with a digit.'
|
' Names must only contain alpha-numeric characters and cannot start with a digit.'
|
||||||
|
Loading…
Reference in New Issue
Block a user