package.utils.validators.slug.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
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