From 9de6e44343351b2be608434918d04f4a564a6091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Sun, 15 May 2022 00:33:58 +0400 Subject: [PATCH] validateAlias to artiste --- src/api/artiste/content-types/artiste/lifecycles.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/api/artiste/content-types/artiste/lifecycles.js b/src/api/artiste/content-types/artiste/lifecycles.js index 8d45dd7..e32d506 100644 --- a/src/api/artiste/content-types/artiste/lifecycles.js +++ b/src/api/artiste/content-types/artiste/lifecycles.js @@ -5,6 +5,7 @@ * to customize this model */ +const { ValidationError } = require("@strapi/utils").errors const slugify = require('slugify') const jwennTeksEpiId = async data => { @@ -17,11 +18,19 @@ const jwennAwtisEpiId = async id => { return artiste } +const validateArtiste = alias => { + if (!alias || alias.trim().length === 0) { + throw new ValidationError('Champ obligatoire. Veuillez choisir un alias.'); + } +} + module.exports = { beforeUpdate: async event => { let {data} = event.params if(!data.publishedAt) { + validateArtiste(data.alias) + if (!data.slug || data.slug !== slugify(data.alias, {lower: true, remove: /[*#+~.()'"!:@]/g})) { data.slug = slugify(data.alias, {lower: true, remove: /[*#+~.()'"!:@]/g}) } @@ -30,6 +39,8 @@ module.exports = { beforeCreate: async event => { let {data} = event.params + validateArtiste(data.alias) + data.slug = slugify(data.alias, {lower: true, remove: /[*#+~.()'"!:@]/g}) }, afterUpdate: async event => {