All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.utils.validators.slug.js Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
import { VALID_SLUG_REGEX } from "../../constants/regex";
import { t } from "i18next";
import * as yup from "yup";
export var slugValidator = function slugValidator(label) {
  return yup.string().test({
    name: "slug",
    test: function test(value, ctx) {
      if (!value) {
        return ctx.createError({
          message: t("neetoCommons.validators.isRequired", {
            what: label
          })
        });
      }
      if (VALID_SLUG_REGEX.test(value)) return true;
      if (/[A-Z]/.test(value)) {
        return ctx.createError({
          message: t("neetoCommons.validators.mustNotContainCapitalLetters", {
            what: label
          })
        });
      }
      if (value !== null && value !== void 0 && value.includes(" ")) {
        return ctx.createError({
          message: t("neetoCommons.validators.mustNotContainSpaces", {
            what: label
          })
        });
      }
      if (!/^[a-z0-9-]+$/.test(value)) {
        return ctx.createError({
          message: t("neetoCommons.validators.mustNotContainSpecialCharactersExceptHyphen", {
            what: label
          })
        });
      }
      if (/^[-]|[-]$/.test(value)) {
        return ctx.createError({
          message: t("neetoCommons.validators.mustNotStartOrEndWithSpecialCharacters", {
            what: label
          })
        });
      }
      return true;
    }
  });
};
//# sourceMappingURL=slug.js.map




© 2015 - 2024 Weber Informatics LLC | Privacy Policy