diff --git a/sitewhere-core-api/src/main/java/com/sitewhere/spi/error/ErrorCode.java b/sitewhere-core-api/src/main/java/com/sitewhere/spi/error/ErrorCode.java index 1a8afa444..0ae5fb367 100644 --- a/sitewhere-core-api/src/main/java/com/sitewhere/spi/error/ErrorCode.java +++ b/sitewhere-core-api/src/main/java/com/sitewhere/spi/error/ErrorCode.java @@ -80,6 +80,9 @@ public enum ErrorCode { /** Indicates 'stop' command issued to tenant that was already stopped */ TenantAlreadyStopped(301, "Tenant was already stopped."), + /** Indicates a tenant id was passed with the wrong format */ + TenantIdFormat(305, "Tenant id should be an alphanumeric value with no spaces."), + /*************************** * INVALID OR DUPLICATE ID * ***************************/ diff --git a/sitewhere-core/src/main/java/com/sitewhere/core/SiteWherePersistence.java b/sitewhere-core/src/main/java/com/sitewhere/core/SiteWherePersistence.java index b34706590..ab10c0ef1 100644 --- a/sitewhere-core/src/main/java/com/sitewhere/core/SiteWherePersistence.java +++ b/sitewhere-core/src/main/java/com/sitewhere/core/SiteWherePersistence.java @@ -188,6 +188,19 @@ protected static void requireNotNull(Object field) throws SiteWhereException { } } + /** + * Requires that a String field be a non null, non space-filled value. + * + * @param field + * @throws SiteWhereException + */ + protected static void requireFormat(String field, String regex, ErrorCode ifFails) throws SiteWhereException { + require(field); + if (!field.matches(regex)) { + throw new SiteWhereSystemException(ifFails, ErrorLevel.ERROR); + } + } + /** * Detect whether the request has an updated value. * @@ -1448,7 +1461,7 @@ public static Tenant tenantCreateLogic(ITenantCreateRequest request) throws Site Tenant tenant = new Tenant(); // Id is required. - require(request.getId()); + requireFormat(request.getId(), "[\\w]*", ErrorCode.TenantIdFormat); tenant.setId(request.getId()); // Name is required. diff --git a/sitewhere-ui/src/components/tenants/TenantDialog.vue b/sitewhere-ui/src/components/tenants/TenantDialog.vue index a266616c2..5d7567b2e 100644 --- a/sitewhere-ui/src/components/tenants/TenantDialog.vue +++ b/sitewhere-ui/src/components/tenants/TenantDialog.vue @@ -20,7 +20,8 @@ + v-model="tenantId" hide-details prepend-icon="info" + :rules="[rules.tenantId]"> @@ -86,6 +87,12 @@ export default { metadata: [], templatesList: [], allUsers: [], + rules: { + tenantId: (value) => { + const pattern = /^[\w]*$/ + return pattern.test(value) || 'Tenant id should be alphanumeric with no spaces.' + } + }, error: null }),