forked from MapComplete/MapComplete
Merge master
This commit is contained in:
commit
80168f5d0d
919 changed files with 95585 additions and 8504 deletions
39
src/UI/InputElement/Validators/ImageUrlValidator.ts
Normal file
39
src/UI/InputElement/Validators/ImageUrlValidator.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import UrlValidator from "./UrlValidator"
|
||||
import { Translation } from "../../i18n/Translation"
|
||||
|
||||
export default class ImageUrlValidator extends UrlValidator {
|
||||
private static readonly allowedExtensions = ["jpg", "jpeg", "svg", "png"]
|
||||
|
||||
constructor() {
|
||||
super(
|
||||
"image",
|
||||
"Same as the URL-parameter, except that it checks that the URL ends with `.jpg`, `.png` or some other typical image format"
|
||||
)
|
||||
}
|
||||
|
||||
private static hasValidExternsion(str: string): boolean {
|
||||
str = str.toLowerCase()
|
||||
return ImageUrlValidator.allowedExtensions.some((ext) => str.endsWith(ext))
|
||||
}
|
||||
|
||||
getFeedback(s: string, _?: () => string): Translation | undefined {
|
||||
const superF = super.getFeedback(s, _)
|
||||
if (superF) {
|
||||
return superF
|
||||
}
|
||||
if (!ImageUrlValidator.hasValidExternsion(s)) {
|
||||
return new Translation(
|
||||
"This URL does not end with one of the allowed extensions. These are: " +
|
||||
ImageUrlValidator.allowedExtensions.join(", ")
|
||||
)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
isValid(str: string): boolean {
|
||||
if (!super.isValid(str)) {
|
||||
return false
|
||||
}
|
||||
return ImageUrlValidator.hasValidExternsion(str)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue