Docs: improve comments, error checking and defaults

This commit is contained in:
Pieter Vander Vennet 2024-01-11 05:10:04 +01:00
parent 65da7155fa
commit ac9792ac43
6 changed files with 47 additions and 3 deletions

View file

@ -27,6 +27,7 @@ import IconValidator from "./Validators/IconValidator"
import TagValidator from "./Validators/TagValidator"
import IdValidator from "./Validators/IdValidator"
import SlopeValidator from "./Validators/SlopeValidator"
import VeloparkValidator from "./Validators/VeloparkValidator"
export type ValidatorType = (typeof Validators.availableTypes)[number]
@ -58,6 +59,7 @@ export default class Validators {
"fediverse",
"id",
"slope",
"velopark"
] as const
public static readonly AllValidators: ReadonlyArray<Validator> = [
@ -86,6 +88,7 @@ export default class Validators {
new FediverseValidator(),
new IdValidator(),
new SlopeValidator(),
new VeloparkValidator()
]
private static _byType = Validators._byTypeConstructor()

View file

@ -0,0 +1,37 @@
import { Translation } from "../../i18n/Translation"
import UrlValidator from "./UrlValidator"
export default class VeloparkValidator extends UrlValidator {
constructor() {
super("velopark", "A custom element to allow copy-pasting velopark-pages")
}
getFeedback(s: string): Translation {
const superF = super.getFeedback(s)
if (superF) {
return superF
}
const url = new URL(s)
if (url.hostname !== "velopark.be" && url.hostname !== "www.velopark.be" && url.hostname !== "data.velopark.be") {
return new Translation({ "*": "Invalid hostname, expected velopark.be" })
}
if(!s.startsWith("https://data.velopark.be/data/") && !s.startsWith("https://www.velopark.be/static/data/")){
return new Translation({"*":"A valid URL should either start with https://data.velopark.be/data/ or https://www.velopark.be/static/data/"})
}
}
public isValid(str: string) {
return super.isValid(str)
}
reformat(str: string): string {
const url = new URL(super.reformat(str))
if(url.pathname.startsWith("/static/data/")){
const id = str.substring(str.lastIndexOf("/")+1)
return "https://data.velopark.be/data/"+id
}
return super.reformat(str)
}
}