validator icon indicating copy to clipboard operation
validator copied to clipboard

use of DEFINED_RULES on docs

Open tiagofrancafernandes opened this issue 1 year ago • 1 comments

I suggest to add DEFINED_RULES use on docs.

This is an example:

import { validate, setLocales, register, en, pt, DEFINED_RULES } from "robust-validator";

const isObjectRule = (value) => {
    return value !== null && typeof value === 'object' && !Array.isArray(value);
};

if (!('object' in DEFINED_RULES)) {
    register( // https://validator.axe-api.com/customization.html
        'object', // The rule name
        isObjectRule, // The rule functions
        // Translations
        {
            en: "The field must be an object",
        },
    );
}

tiagofrancafernandes avatar Oct 10 '24 22:10 tiagofrancafernandes

@tiagofrancafernandes thanks for the suggestion. ❤️ It's a very good idea.

But what do you think to provide a function that tells you if a rule is defined or not? An example;

import { register, isRegistered } from "robust-validator";

const isObjectRule = (value) => {
  return value !== null && typeof value === "object" && !Array.isArray(value);
};

if (!isRegistered("object")) {
  register("object", isObjectRule, {
    en: "The field must be an object",
  });
}

Also, I liked the object rule you defined. I think it would be a good idea to add that rule internally. What do you think? ☺️

ozziest avatar Oct 11 '24 14:10 ozziest