Skip to content

Commit

Permalink
feat(sdl): move endpoint validation from cloudmos (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed May 3, 2024
1 parent c23d43a commit 1406990
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/error/CustomValidationError.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class CustomValidationError extends Error {
static assert(condition: boolean, message: string): asserts condition {
static assert(condition: unknown, message: string): asserts condition {
if (!condition) {
throw new CustomValidationError(message);
}
Expand Down
18 changes: 18 additions & 0 deletions src/sdl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ export class SDL {
}
}

private readonly ENDPOINT_NAME_VALIDATION_REGEX = /^[a-z]+[-_\da-z]+$/;

private readonly ENDPOINT_KIND_IP = "ip";

constructor(
public readonly data: v2Sdl,
public readonly version: NetworkVersion = "beta2",
Expand All @@ -156,6 +160,7 @@ export class SDL {
});

this.validateDenom();
this.validateEndpoints();
}

private validateDenom() {
Expand All @@ -168,6 +173,19 @@ export class SDL {
CustomValidationError.assert(!invalidDenom, `Invalid denom: "${invalidDenom}". Only uakt and ${usdcDenom} are supported.`);
}

private validateEndpoints() {
if (!this.data.endpoints) {
return;
}

Object.keys(this.data.endpoints).forEach(endpointName => {
const endpoint = this.data.endpoints[endpointName];
CustomValidationError.assert(this.ENDPOINT_NAME_VALIDATION_REGEX.test(endpointName), `Endpoint named "${endpointName}" is not a valid name.`);
CustomValidationError.assert(!!endpoint.kind, `Endpoint named "${endpointName}" has no kind.`);
CustomValidationError.assert(endpoint.kind === this.ENDPOINT_KIND_IP, `Endpoint named "${endpointName}" has an unknown kind "${endpoint.kind}".`);
});
}

services() {
if (this.data) {
return this.data.services;
Expand Down

0 comments on commit 1406990

Please sign in to comment.