From 0472e96c6df6d0b4b19ce8c9340f3dd4e7f8ac49 Mon Sep 17 00:00:00 2001 From: amentis Date: Thu, 30 May 2024 16:22:08 +0300 Subject: [PATCH] add tenant persist code validation pattern --- .../org/opencdmp/model/persist/TenantPersist.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/TenantPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/TenantPersist.java index 87279c56b..033f6ce5f 100644 --- a/backend/core/src/main/java/org/opencdmp/model/persist/TenantPersist.java +++ b/backend/core/src/main/java/org/opencdmp/model/persist/TenantPersist.java @@ -15,6 +15,7 @@ import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; import java.util.UUID; +import java.util.regex.Pattern; public class TenantPersist { @@ -115,6 +116,10 @@ public class TenantPersist { .iff(() -> !this.isEmpty(item.getCode())) .must(() -> this.lessEqualLength(item.getCode(), TenantEntity._codeLength)) .failOn(TenantPersist._code).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{TenantPersist._code}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isEmpty(item.getCode())) + .must(() -> this.validateCodePattern(item.getCode())) + .failOn(TenantPersist._code).failWith(messageSource.getMessage("Validation_UnexpectedValue", new Object[]{TenantPersist._code}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isEmpty(item.getName())) .failOn(TenantPersist._name).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantPersist._name}, LocaleContextHolder.getLocale())), @@ -128,6 +133,13 @@ public class TenantPersist { ); } + + private boolean validateCodePattern(String code){ + if (this.isEmpty(code)) return false; + + Pattern pattern = Pattern.compile("^[a-z0-9_]*$"); + return pattern.matcher(code).matches(); + } } }