Skip to content

Commit

Permalink
feat: contract validation with wild cards
Browse files Browse the repository at this point in the history
  • Loading branch information
roshaans committed Jun 2, 2023
1 parent cf8274c commit 42443eb
Show file tree
Hide file tree
Showing 3 changed files with 19 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 { validateAccountId } 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 = validateAccountId(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
12 changes: 12 additions & 0 deletions frontend/src/utils/validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

const CONTRACT_NAME_REGEX = RegExp(/^(\*|([a-z\d]+[-_])*[a-z\d]+)(\.*(\*|([a-z\d]+[-_])*[a-z\d]+))*\.(testnet|near)$/)

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


0 comments on commit 42443eb

Please sign in to comment.