axe-api / validator

Rule-based data validation library in Node.js
https://validator.axe-api.com
MIT License
18 stars 1 forks source link

Nullable rule #43

Closed GoldraK closed 7 months ago

GoldraK commented 7 months ago

Hello, First of all, thank you for the package, it works very well.

I'm trying to add a new rule for null cases but I can't get it to work.

I can't figure out why.

const isNullable = (value) => {
    if (value === null  || value === 'null') {
        return true;
    }
    return false;
};

register(
    'nullable',
    isNullable,
    {
        en: "The {0} field must be null or empty."
    }
)

All the best.

ozziest commented 7 months ago

Hi @GoldraK . Thank you very much for the feedback. 🤗

But I couldn't understand the issue clearly? Do you get any errors? Or just isn't it working as expected?

ozziest commented 7 months ago

@GoldraK I think I found the issue. You need to set which languages are supported first. After that yo can register your custom rules.

import { setLocales, register } from "robust-validator";
import en from "robust-validator/dist/i18n/en.json";

setLocales(en);

const isNullable = (value) => {
    if (value === null  || value === 'null') {
        return true;
    }
    return false;
};

register(
    'nullable',
    isNullable,
    {
        en: "The {0} field must be null or empty."
    }
)

Please let me know if that doesn't solve your problem.