27 lines
562 B
JavaScript
27 lines
562 B
JavaScript
import { ValidationError } from "../ValidationError.js";
|
|
import { Mechanism } from "./Mechanism.js";
|
|
|
|
export class IPv4Mechanism extends Mechanism {
|
|
placeholder = "0.0.0.0";
|
|
|
|
constructor(key) {
|
|
super(key);
|
|
}
|
|
|
|
validate(value) {
|
|
const segments = value.split(".");
|
|
|
|
const valid = segments.every(segment => {
|
|
const number = parseInt(segment);
|
|
|
|
return !isNaN(number) && number >= 0 && number <= 255;
|
|
});
|
|
|
|
if (segments.length !== 4 || !valid) {
|
|
throw new ValidationError(`Value for ${key} is not a valid IPv4 address`);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|