mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-19 11:42:52 +00:00
Addressed PR comments
This commit is contained in:
@@ -3,26 +3,40 @@ import z from "zod";
|
||||
|
||||
import { CharacterType, characterValidator } from "@app/lib/validator/validate-string";
|
||||
|
||||
export const validateTemplateRegexField = z
|
||||
.string()
|
||||
.min(1)
|
||||
.max(100)
|
||||
.refine(
|
||||
(val) =>
|
||||
characterValidator([
|
||||
CharacterType.AlphaNumeric,
|
||||
CharacterType.Spaces, // (space)
|
||||
CharacterType.Asterisk, // *
|
||||
CharacterType.At, // @
|
||||
CharacterType.Hyphen, // -
|
||||
CharacterType.Period, // .
|
||||
CharacterType.Backslash // \
|
||||
])(val),
|
||||
{
|
||||
message: "Invalid pattern: only alphanumeric characters, spaces, *, ., @, -, and \\ are allowed."
|
||||
}
|
||||
)
|
||||
// we ensure that the inputted pattern is computationally safe by limiting star height to 1
|
||||
.refine((v) => safe(v), {
|
||||
message: "Unsafe REGEX pattern"
|
||||
});
|
||||
export const createTemplateFieldValidator = (options?: {
|
||||
minLength?: number;
|
||||
maxLength?: number;
|
||||
allowedCharacters?: CharacterType[];
|
||||
customMessage?: string;
|
||||
}) => {
|
||||
const {
|
||||
minLength = 1,
|
||||
maxLength = 100,
|
||||
allowedCharacters = [
|
||||
CharacterType.AlphaNumeric,
|
||||
CharacterType.Spaces, // (space)
|
||||
CharacterType.Asterisk, // *
|
||||
CharacterType.At, // @
|
||||
CharacterType.Hyphen, // -
|
||||
CharacterType.Period, // .
|
||||
CharacterType.Backslash // \
|
||||
],
|
||||
customMessage = "Invalid pattern: only alphanumeric characters, spaces, *, ., @, -, and \\ are allowed."
|
||||
} = options || {};
|
||||
|
||||
return (
|
||||
z
|
||||
.string()
|
||||
.min(minLength)
|
||||
.max(maxLength)
|
||||
.refine((val) => characterValidator(allowedCharacters)(val), {
|
||||
message: customMessage
|
||||
})
|
||||
// we ensure that the inputted pattern is computationally safe by limiting star height to 1
|
||||
.refine((v) => safe(v), {
|
||||
message: "Unsafe REGEX pattern"
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
export const validateTemplateRegexField = createTemplateFieldValidator();
|
||||
|
||||
Reference in New Issue
Block a user