Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DPLT-1009] feat: contract validation with wild cards #93

Merged
merged 3 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
);
}