Page MenuHomeSealhub

url.ts
No OneTemporary

import { URL } from "url";
import type { ValidationResult } from "../../../chip-types/field.js";
import { Field, Context } from "../../../main.js";
export type UrlParams = Partial<{
allowed_origins: string[];
allowed_protocols: string[];
}>;
export default class Url extends Field<string> {
typeName = "url";
params: UrlParams;
constructor(params: UrlParams = {}) {
super();
this.params = params;
}
async isProperValue(
context: Context,
value: string
): Promise<ValidationResult> {
const allowed_protocols = this.params.allowed_protocols
? this.params.allowed_protocols.map((protocol) => `${protocol}:`)
: [];
let url;
try {
url = new URL(value);
} catch (err) {
return Field.invalid(err);
}
if (
this.params.allowed_origins &&
!this.params.allowed_origins.includes(url.hostname)
) {
return Field.invalid(
context.app.i18n("not_allowed_domain", [
url.hostname,
this.params.allowed_origins,
])
);
}
if (
this.params.allowed_protocols &&
!allowed_protocols.includes(url.protocol)
) {
return Field.invalid(
context.app.i18n("not_allowed_protocol", [
url.protocol,
this.params.allowed_protocols,
])
);
}
return Field.valid();
}
}

File Metadata

Mime Type
text/x-java
Expires
Wed, May 7, 19:41 (1 d, 14 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
636292
Default Alt Text
url.ts (1 KB)

Event Timeline