Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property is always required even when my When() condition isn't met. #627

Closed
CodisRedding opened this issue Apr 9, 2015 · 0 comments
Closed
Labels
bug Bug or defect
Milestone

Comments

@CodisRedding
Copy link

I cannot get my test to pass validation. Even though I'm setting the When() condition to only required the property if a certain condition is met, it's always requiring it. (See infantYouthParentsName property below)

Test:

  lab.beforeEach((done) => {

   let act = {
      id: '123456789012345',
      name: undefined,
      location: undefined,
      for: undefined,
      address1: undefined,
      address2: undefined,
      baptismAge: undefined,
      city: undefined,
      clergyPhoneNumber: undefined,
      contactOrg: undefined,
      contact: undefined,
      country: undefined,
      dateActPerformed: undefined,
      dateOfBirth: undefined,
      infantYouthParentsName: undefined,
      remarks: undefined,
      state: undefined,
      stateOther: undefined,
      transferDate: undefined,
      witness1Name: undefined,
      witness2Name: undefined,
      zipcode: undefined
    };

    done();
 });

lab.test('Name should pass with a valid choice', (done) => {

    act.name = 'Transfer';

    ValidAct.validateExisting(act)
      .then((val) => {
        Code.expect(val).to.equal(act);
        done();
      })
      .catch((err) => {
        // child "infantYouthParentsName" fails because ["infantYouthParentsName" is required]
        done(err);
      });
});

Error:

child "infantYouthParentsName" fails because ["infantYouthParentsName" is required]

Source

'use strict';

import Bluebird from 'bluebird';
import Joi from 'joi';

let _names = ['Baptism', 'Profession of Faith', 'Transfer'];
let _ages = ['Adult', 'Infant/Youth'];
let _countries = ['UNITED STATES OF AMERICA', 'AFGHANISTAN', 'ALBANIA', 'ALGERIA', 'AMERICAN SAMOA', 'ANDORRA', 'ANGOLA', 'ANGUILLA', 'ANTIGUA & BARBUDA', 'ARGENTINA', 'ARMENIA', 'ARUBA', 'AUSTRALIA', 'AUSTRIA', 'AZERBAIJAN', 'BAHAMAS', 'BAHRAIN', 'BANGLADESH', 'BARBADOS', 'BELARUS', 'BELGIUM', 'BELIZE', 'BENIN', 'BERMUDA', 'BHUTAN', 'BOLIVIA', 'BOSNIA AND HERZEGOVINA', 'BOTSWANA', 'BRAZIL', 'BRITISH VIRGIN ISLANDS', 'BRUNEI DARUSSALAM', 'BULGARIA', 'BURKINA FASO', 'BURUNDI', 'CAMBODIA', 'CAMEROON', 'CANADA', 'CAPE VERDE', 'CAYMAN ISLANDS', 'CENTRAL AFRICAN REPUBLIC', 'CHAD', 'CHANNEL ISLANDS', 'CHILE', 'CHINA', 'COCOS ISLANDS', 'COLOMBIA', 'COMOROS', 'COOK ISLANDS', 'COSTA RICA', 'COTE D\'IVOIRE', 'CROATIA', 'CUBA', 'CYPRUS', 'CZECH REPUBLIC', 'DEMOCRATIC REPUBLIC OF THE CONGO', 'DENMARK', 'DJIBOUTI', 'DOMINICA', 'DOMINICAN REPUBLIC', 'EAST TIMOR', 'ECUADOR', 'EGYPT', 'EL SALVADOR', 'EQUATORIAL GUINEA', 'ERITREA', 'ESTONIA', 'ETHIOPIA', 'FALKLAND ISLANDS', 'FAROE ISLANDS', 'FIJI', 'FINLAND', 'FRANCE', 'FRENCH GUIANA', 'FRENCH POLYNESIA', 'GABON', 'GAMBIA', 'GEORGIA', 'GERMANY', 'GHANA', 'GIBRALTAR', 'GREECE', 'GREENLAND', 'GRENADA', 'GUADELOUPE', 'GUAM', 'GUATEMALA', 'GUIANA, FRENCH', 'GUINEA', 'GUINEA-BISSAU', 'GUYANA', 'HAITI', 'HONDURAS', 'HONG KONG', 'HUNGARY', 'ICELAND', 'INDIA', 'INDONESIA', 'IRAN', 'IRAQ', 'IRELAND', 'ISLE OF MAN', 'ISRAEL', 'ITALY', 'JAMAICA', 'JAPAN', 'JORDAN', 'KAZAKHSTAN', 'KENYA', 'KIRIBATI', 'KOSOVO', 'KUWAIT', 'KYRGYZSTAN', 'LAOS', 'LATVIA', 'LEBANON', 'LESOTHO', 'LIBERIA', 'LIBYA', 'LIECHTENSTEIN', 'LITHUANIA', 'LUXEMBOURG', 'MACAO', 'MACEDONIA', 'MADAGASCAR', 'MALAWI', 'MALAYSIA', 'MALDIVES', 'MALI', 'MALTA', 'MARSHALL ISLANDS', 'MARTINIQUE', 'MAURITANIA', 'MAURITIUS', 'MAYOTTE', 'MEXICO', 'MICRONESIA', 'MOLDOVA', 'MONACO', 'MONGOLIA', 'MONTENEGRO', 'MONTSERRAT', 'MOROCCO', 'MOZAMBIQUE', 'MYANMAR', 'NAMIBIA', 'NAURU', 'NEPAL', 'NETHERLANDS', 'NETHERLANDS ANTILLES', 'NEW CALEDONIA', 'NEW ZEALAND', 'NICARAGUA', 'NIGER', 'NIGERIA', 'NORTHERN MARIANA ISLANDS', 'NORTH KOREA', 'NORWAY', 'OMAN', 'PAKISTAN', 'PALAU', 'PALESTINIAN TERRITORY', 'PANAMA', 'PAPUA NEW GUINEA', 'PARAGUAY', 'PERU', 'PHILIPPINES', 'PITCAIRN ISLAND', 'POLAND', 'PORTUGAL', 'PUERTO RICO', 'QATAR', 'REPUBLIC OF CONGO', 'REUNION', 'ROMANIA', 'RUSSIAN FEDERATION', 'RWANDA', 'SAINT HELENA', 'SAINT KITTS AND NEVIS', 'SAINT LUCIA', 'SAINT MAARTEN', 'SAINT VINCENT AND THE GRENADINES', 'SAN MARINO', 'SAO TOME AND PRINCIPE', 'SAUDI ARABIA', 'SENEGAL', 'SERBIA', 'SEYCHELLES', 'SIERRA LEONE', 'SINGAPORE', 'SLOVAKIA', 'SLOVENIA', 'SOLOMON ISLANDS', 'SOMALIA', 'SOUTH AFRICA', 'SOUTH KOREA', 'SPAIN', 'SRI LANKA', 'SUDAN', 'SURINAME', 'SWAZILAND', 'SWEDEN', 'SWITZERLAND', 'SYRIA', 'TAIWAN', 'TAJIKISTAN', 'TANZANIA', 'THAILAND', 'TOGO', 'TONGA', 'TRINIDAD & TOBAGO', 'TUNISIA', 'TURKEY', 'TURKMENISTAN', 'TURKS AND CAICOS ISLANDS', 'TUVALU', 'UGANDA', 'UKRAINE', 'UNITED ARAB EMIRATES', 'UNITED KINGDOM', 'UNITED STATES VIRGIN ISLANDS', 'URUGUAY', 'UZBEKISTAN', 'VANUATU', 'VATICAN CITY', 'VENEZUELA', 'VIET NAM', 'WALLIS AND FUTUNA ISLANDS', 'WESTERN SAMOA', 'YEMEN', 'ZAMBIA', 'ZIMBABWE', 'AE', 'AP', 'AA', 'OTHER', 'N/A'];
let _states = ['AK', 'AL', 'AR', 'AS', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'FM', 'GA', 'GU', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MH', 'MI', 'MN', 'MO', 'MP', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'PR', 'PW', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VI', 'VT', 'WA', 'WI', 'WV', 'WY', 'AB', 'BC', 'MB', 'NB', 'NL', 'NT', 'NS', 'NU', 'ON', 'PE', 'QC', 'SK', 'YT', 'N/A', 'Other/Specify'];

let msgContactOrg = 'Invalid Contact Organization';
let msgContact = 'Invalid Contact';
let msgId = 'Invalid Id';
let msgStateOther = 'Invalid State Other';

let _schemaNew = Joi.object().keys({
  name: Joi.string().valid(_names),
  location: Joi.string().max(200),
  for: Joi.string().max(150),
  address1: Joi.string().max(150),
  address2: Joi.string().max(150),
  baptismAge: Joi.any()
    .when('name', {
      is: 'Baptism',
      then: Joi.valid(_ages).required(),
      otherwise: Joi.forbidden()
    }),
  city: Joi.string().max(100),
  clergyPhoneNumber: Joi.string().max(25),
  contactOrg: Joi.string().min(15).max(15),
  country: Joi.string().valid(_countries),
  contact: Joi.string().min(15).max(18),
  dateActPerformed: Joi.date(),
  dateOfBirth: Joi.date(),
  infantYouthParentsName: Joi.string()
    .when('baptismAge', {
      is: 'Infant/Youth',
      then: Joi.string().max(150).required(),
      otherwise: Joi.string().max(150)
    }),
  remarks: Joi.string().max(255),
  state: Joi.string().allow(_states),
  stateOther: Joi.string()
    .when('state', {
      is: 'Other/Specify',
      then: Joi.string().max(100),
      otherwise: Joi.string().max(100)
    }),
  transferDate: Joi.date()
    .when('name', {
      is: 'Transfer',
      then: Joi.date()
    }),
  witness1Name: Joi.string(),
  witness2Name: Joi.string().max(150),
  zipcode: Joi.string().max(25)
});

let _schemaId = Joi.object().keys({
  id: Joi.string().min(15).max(18).required()
});

let _schemaExisting = _schemaNew.concat(_schemaId);


export default {

  validate: Bluebird.promisify(Joi.validate),

  validateExisting(act) {
    return this.validate(act, _schemaExisting);
  },

  validateNew(act) {
    return this.validate(act, _schemaNew);
  },

  validateId(act) {
    return this.validate(act, _schemaId);
  }
}
@Marsup Marsup added the bug Bug or defect label Apr 9, 2015
@Marsup Marsup self-assigned this Apr 9, 2015
@Marsup Marsup added this to the 6.1.1 milestone Apr 9, 2015
@Marsup Marsup closed this as completed in f11c2c6 Apr 9, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants