/** @format */
import {ValidationRule} from "antd/es/form"
import {ipPattern} from '../const/regular';
/**
 * IP白名单验证
 * @param _
 * @param value
 * @param callback
 */
export const validatorIp = (_: ValidationRule, value: string, callback: (msg?: string) => void) => {
    if (!value) {
        return callback();
    }
    const ips = value.split(',');
    let validateIp = true;
    ips.forEach(element => {
        if (element.includes('-')) {
            const rangeIp = element.split('-');
            rangeIp.forEach(item => {
                validateIp = ipPattern.test(item);
            });
            if (rangeIp[0] >= rangeIp[1]) {
                validateIp = false;
            }
        } else if (validateIp) {
            validateIp = ipPattern.test(element);
        }
    });
    if (!validateIp) {
        callback('请输入正确的ip地址!');
    }
    callback();
};

