Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
5
Indexable
Never
const regexData = /^\d{2}\/\d{2}\/\d{4}$/;


const validRate = (rate) => {
  if (typeof rate === 'undefined') {
    return { message: 'O campo "rate" é obrigatório' };
  }
  if (rate < 1 || rate > 5 || !Number.isInteger(rate)) {
    return { message: 'O campo "rate" deve ser um número inteiro entre 1 e 5' };
  }
  return {};
};

const validTalk = (talk) => {
  if (!talk) {
    return { message: 'O campo "talk" é obrigatório' };
  }
  if (!talk.watchedAt) {
    return { 
      message: 'O campo "watchedAt" é obrigatório',
    };
  }
  if (!regexData.test(talk.watchedAt)) {
    return { 
      message: 'O campo "watchedAt" deve ter o formato "dd/mm/aaaa"',
    };
  }
  if (validRate(talk.rate)) {
    return validRate(talk.rate);
  }
};
const validTalker = (talker) => {
  const name = validName(talker.name);
  const age = validAge(talker.age);
  const talk = validTalk(talker.talk);
  if (name.message) {
    return name;
  }
  if (age.message) {
    return age;
  }
  if (talk.message && talk) {
    return talk;
  }
  return {};
};
 
module.exports = {
  validTalker,
  validRate,
  validTalk,
};
Leave a Comment