Skip to content

Commit

Permalink
[DPLT-1009] feat: contract validation with wild cards (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
roshaans authored and gabehamilton committed Jun 26, 2023
1 parent 956d41b commit 1342f23
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
17 changes: 6 additions & 11 deletions frontend/src/components/Editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import EditorButtons from "./EditorButtons";
import { PublishModal } from "../Modals/PublishModal";
import {getLatestBlockHeight} from "../../utils/getLatestBlockHeight";
const BLOCKHEIGHT_LIMIT = 3600;
import { validateContractId } from "../../utils/validators"

const contractRegex = RegExp(
"^(([a-zd]+[-_])*[a-zd]+.)*([a-zd]+[-_])*[a-zd]+$"
);

const Editor = ({
options,
Expand Down Expand Up @@ -299,17 +297,14 @@ const Editor = ({
}

function handleSetContractFilter(e) {
// check if contract filter is greater than 2 and less than or equal to 64 chars
const contractFilter = e.target.value;
setContractFilter(contractFilter);
if (
contractFilter.length > 64 ||
contractFilter.length < 2 ||
!contractRegex.test(contractFilter)
) {
setIsContractFilterValid(false);
} else {
const isValid = validateContractId(contractFilter);

if (isValid) {
setIsContractFilterValid(true);
} else {
setIsContractFilterValid(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const BlockHeightOptions = ({
<Form.Control
value={contractFilter}
onChange={handleSetContractFilter}
isValid={isContractFilterValid}
type="text"
placeholder="social.near"
required={true}
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/utils/validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const CONTRACT_NAME_REGEX = RegExp(/^(\*|([a-z\d]+[-_])*[a-z\d]+)(\.*(\*|([a-z\d]+[-_])*[a-z\d]+))*\.(\w+)$/)A

export function validateContractId(accountId) {
return (
accountId.length >= 2 &&
accountId.length <= 64 &&
CONTRACT_NAME_REGEX.test(accountId)
);
}

0 comments on commit 1342f23

Please sign in to comment.