Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

change refine to use define zod objects so the docs are right

adilson61a4b018e154
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index 9abdded034..e890ee77c2 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -4033,18 +4033,21 @@ export const RELAYS = {
export const SECRET_VALIDATION_RULES = {
RULE: {
- type: "The kind of secret the rule applies to. Determines which fields the rule accepts and where the constraints are enforced: `static-secrets` constraints run on write, while `dynamic-secrets` and `secret-rotations` constraints shape the generated password.",
+ type: "The kind of secret the rule applies to. Determines which fields the rule accepts and where the constraints are enforced: `static-secrets` constraints run on write, while `dynamic-secrets` and `secret-rotations` constraints shape the generated credential.",
constraints:
- "The constraints enforced by this rule. Each constraint names what it checks (`type`), what it applies to (`appliesTo`), and its `value` — e.g. the minimum character count for `min-length` or the pattern for `regex-pattern`.",
+ "The constraints enforced by this rule. Each constraint names what it checks (`type`), what it applies to (`appliesTo`), and its `value`, e.g. the minimum character count for `min-length` or the pattern for `regex-pattern`.",
dynamicSecretProviders:
"The dynamic secret providers this rule applies to. A lease is only constrained when its provider is listed here.",
secretRotationProviders:
"The secret rotation providers this rule applies to. A rotation is only constrained when its provider is listed here.",
- appliesTo: "What the constraint checks: the secret key, the secret value, or the generated password.",
- constraintType:
- "The kind of check this constraint performs, e.g. `min-length`, `regex-pattern`, `required-prefix`.",
+ appliesToStatic: "What the constraint checks: the secret key or the secret value.",
+ appliesToGenerated: "What the constraint checks: the generated credential.",
+ constraintTypeStatic:
+ "The kind of check this constraint performs, e.g. `min-length`, `regex-pattern`, `required-prefix`, or `prevent-value-reuse`.",
+ constraintTypeGenerated:
+ "The kind of check this constraint performs, e.g. `min-length`, `regex-pattern`, or `required-prefix`.",
constraintValue:
- "The value the constraint is checked against — e.g. the minimum length, the regex pattern, or the required prefix/suffix string."
+ "The value the constraint is checked against, e.g. the minimum length, the regex pattern, or the required prefix/suffix string."
},
LIST: {
projectId: "The ID of the project to list secret validation rules for."
@@ -4066,7 +4069,7 @@ export const SECRET_VALIDATION_RULES = {
environmentSlug:
"The slug of the environment this rule is scoped to. Omit to leave the current scope unchanged; pass `null` to make the rule apply to every environment in the project.",
secretPath: "The secret path this rule is scoped to.",
- rule: "The rule configuration: which secret type it targets and the constraints to enforce. Replaces the existing configuration as a whole — omit to leave it untouched.",
+ rule: "The rule configuration: which secret type it targets and the constraints to enforce. Replaces the existing configuration as a whole, or omits to leave it untouched.",
isActive: "Whether the secret validation rule is active."
},
DELETE: {
diff --git a/backend/src/services/secret-validation-rule/secret-validation-rule-schemas.ts b/backend/src/services/secret-validation-rule/secret-validation-rule-schemas.ts
index 4d45f9f785..6890fefdb9 100644
--- a/backend/src/services/secret-validation-rule/secret-validation-rule-schemas.ts
+++ b/backend/src/services/secret-validation-rule/secret-validation-rule-schemas.ts
@@ -14,55 +14,41 @@ import {
export const MAX_PREVENT_VALUE_REUSE_VERSIONS = 25;
-// Targets that constraints are allowed to apply to per rule type.
-// Generated-credential rules currently only target the generated password
-// (the username slot is reserved for future use).
-const STATIC_RULE_TARGETS = new Set<ConstraintTarget>([ConstraintTarget.SecretKey, ConstraintTarget.SecretValue]);
-const GENERATED_RULE_TARGETS = new Set<ConstraintTarget>([ConstraintTarget.GeneratedPassword]);
-
-// Constraint types disallowed on generated-credential rules (dynamic secrets
-// and secret rotations). PreventValueReuse is intentionally static-secret-only:
-// for dynamic secrets it has no anchor (each lease is independent and ephemeral),
-// and for rotations we explicitly want users to drive uniqueness through
-// password generation (length/regex) rather than a reuse check that would
-// surface a rotation as a failed lease at issue time.
-const GENERATED_RULE_DISALLOWED_CONSTRAINTS = new Set<ConstraintType>([ConstraintType.PreventValueReuse]);
-
-const baseConstraintSchema = z.object({
- type: z.nativeEnum(ConstraintType).describe(SECRET_VALIDATION_RULES.RULE.constraintType),
- appliesTo: z.nativeEnum(ConstraintTarget).describe(SECRET_VALIDATION_RULES.RULE.appliesTo),
- value: z.string().describe(SECRET_VALIDATION_RULES.RULE.constraintValue)
-});
+const STATIC_RULE_TARGETS = [ConstraintTarget.SecretKey, ConstraintTarget.SecretValue] as const;
+const GENERATED_RULE_TARGETS = [ConstraintTarget.GeneratedPassword] as const;
+
+const GENERATED_CONSTRAINT_TYPES = [
+ ConstraintType.MinLength,
+ ConstraintType.MaxLength,
+ ConstraintType.RegexPattern,
+ ConstraintType.RequiredPrefix,
+ ConstraintType.RequiredSuffix
+] as const;
+const STATIC_CONSTRAINT_TYPES = [...GENERATED_CONSTRAINT_TYPES, ConstraintType.PreventValueReuse] as const;
+
+/** Embed description + example so Mintlify/OpenAPI curl samples include enum fields. */
+const openApiField = (description: string, example: string) => JSON.stringify({ description, example });
+
+type TConstraintInput = {
+ type: ConstraintType;
+ appliesTo: ConstraintTarget;
+ value: string;
+};
-const valueRequiredRefinement = (c: z.infer<typeof baseConstraintSchema>) =>
+const valueRequiredRefinement = (c: TConstraintInput) =>
c.type === ConstraintType.PreventValueReuse || c.value.length > 0;
-const preventValueReuseTargetRefinement = (c: z.infer<typeof baseConstraintSchema>) =>
+const preventValueReuseTargetRefinement = (c: TConstraintInput) =>
c.type !== ConstraintType.PreventValueReuse || c.appliesTo === ConstraintTarget.SecretValue;
-const preventValueReuseRangeRefinement = (c: z.infer<typeof baseConstraintSchema>) => {
+const preventValueReuseRangeRefinement = (c: TConstraintInput) => {
if (c.type !== ConstraintType.PreventValueReuse) return true;
const num = Number(c.value);
return Number.isInteger(num) && num >= 1 && num <= MAX_PREVENT_VALUE_REUSE_VERSIONS;
};
-const buildConstraintSchemaForRuleType = (ruleType: SecretValidationRuleType) => {
- const allowedTargets =
- ruleType === SecretValidationRuleType.StaticSecrets ? STATIC_RULE_TARGETS : GENERATED_RULE_TARGETS;
- const disallowedTypes =
- ruleType === SecretValidationRuleType.StaticSecrets
- ? new Set<ConstraintType>()
- : GENERATED_RULE_DISALLOWED_CONSTRAINTS;
-
- return baseConstraintSchema
- .refine((c) => allowedTargets.has(c.appliesTo), {
- message: `Constraint target is not allowed for ${ruleType} rules`,
- path: ["appliesTo"]
- })
- .refine((c) => !disallowedTypes.has(c.type), {
- message: `Constraint type is not supported for ${ruleType} rules`,
- path: ["type"]
- })
+const withConstraintRefinements = <T extends z.ZodType<TConstraintInput>>(schema: T) =>
+ schema
.refine(valueRequiredRefinement, { message: "Value is required", path: ["value"] })
.refine(preventValueReuseTargetRefinement, {
message: "No value reuse constraint can only apply to secret values",
@@ -72,6 +58,33 @@ const buildConstraintSchemaForRuleType = (ruleType: SecretValidationRuleType) =>
message: `Prevent value reuse version count must be between 1 and ${MAX_PREVENT_VALUE_REUSE_VERSIONS}`,
path: ["value"]
});
+
+const buildConstraintSchemaForRuleType = (ruleType: SecretValidationRuleType) => {
+ if (ruleType === SecretValidationRuleType.StaticSecrets) {
+ return withConstraintRefinements(
+ z.object({
+ type: z
+ .enum(STATIC_CONSTRAINT_TYPES)
+ .describe(openApiField(SECRET_VALIDATION_RULES.RULE.constraintTypeStatic, ConstraintType.MinLength)),
+ appliesTo: z
+ .enum(STATIC_RULE_TARGETS)
+ .describe(openApiField(SECRET_VALIDATION_RULES.RULE.appliesToStatic, ConstraintTarget.SecretValue)),
+ value: z.string().describe(openApiField(SECRET_VALIDATION_RULES.RULE.constraintValue, "8"))
+ })
+ );
+ }
+
+ return withConstraintRefinements(
+ z.object({
+ type: z
+ .enum(GENERATED_CONSTRAINT_TYPES)
+ .describe(openApiField(SECRET_VALIDATION_RULES.RULE.constraintTypeGenerated, ConstraintType.MinLength)),
+ appliesTo: z
+ .enum(GENERATED_RULE_TARGETS)
+ .describe(openApiField(SECRET_VALIDATION_RULES.RULE.appliesToGenerated, ConstraintTarget.GeneratedPassword)),
+ value: z.string().describe(openApiField(SECRET_VALIDATION_RULES.RULE.constraintValue, "8"))
+ })
+ );
};
export const staticSecretsInputsSchema = z.object({