Skip to content

Commit

Permalink
Merge pull request wso2#4472 from amanda-ariyaratne/patch-3
Browse files Browse the repository at this point in the history
Update role name field input validation
  • Loading branch information
Achintha Isuru authored Nov 1, 2023
2 parents 7cf21cf + 9beb9ab commit 5f8eb64
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/little-pens-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@wso2is/console": patch
"@wso2is/form": patch
"@wso2is/validation": patch
---

Update role name field input validation
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const BasicRoleDetails: FunctionComponent<BasicRoleProps> = (props: Basic
<Field.Input
ariaLabel="roleName"
name="roleName"
inputType="identifier"
inputType="roleName"
required={ true }
readOnly={ isReadOnly }
value={ role?.displayName }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export const RoleBasics: FunctionComponent<RoleBasicProps> = (props: RoleBasicPr
>
<Field.Input
ariaLabel="roleName"
inputType="identifier"
inputType="roleName"
data-componentid={ `${ componentId }-role-name-input` }
type="text"
name="roleName"
Expand Down
5 changes: 3 additions & 2 deletions modules/form/src/components/field-input.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2021, WSO2 LLC. (https://www.wso2.com).
* Copyright (c) 2021-2023, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -44,7 +44,8 @@ export interface FieldInputPropsInterface extends FormFieldPropsInterface {
| "url"
| "copy_input"
| "password"
| "phoneNumber";
| "phoneNumber"
| "roleName";
/**
* Hint of the form field.
*/
Expand Down
8 changes: 7 additions & 1 deletion modules/form/src/utils/validate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2021, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
* Copyright (c) 2021-2023, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -82,6 +82,12 @@ export const getDefaultValidation = (
return FieldConstants.INVALID_SCOPES_ERROR;
}

break;
case "roleName":
if (!FormValidation.isValidRoleName(value)) {
return FieldConstants.INVALID_NAME_ERROR;
}

break;
}
}
Expand Down
4 changes: 3 additions & 1 deletion modules/validation/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
* Copyright (c) 2019-2023, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -27,6 +27,7 @@ import {
isValidDescription,
isValidResourceKey,
isValidResourceName,
isValidRoleName,
mobileNumber,
resourceName,
scopes,
Expand All @@ -43,6 +44,7 @@ export const FormValidation = {
isValidDescription,
isValidResourceKey,
isValidResourceName,
isValidRoleName,
mobileNumber,
resourceName,
scopes,
Expand Down
22 changes: 21 additions & 1 deletion modules/validation/src/validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
* Copyright (c) 2019-2023, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -314,3 +314,23 @@ export const isValidResourceKey = (value: string): boolean => {
return false;
}
};

/**
* This validates the role name. Returns true if valid. False if not valid.
*
* @param value - Input to be validated.
* @returns whether input is accepted or not.
*/
export const isValidRoleName = (value: string): boolean => {
try {
const result: ValidationResult = Joi.string()
.regex(new RegExp("^[a-zA-Z][a-zA-Z0-9-_. ]+$"))
.min(3)
.max(255)
.validate(value);

return !result.error;
} catch (error) {
return false;
}
};

0 comments on commit 5f8eb64

Please sign in to comment.