const moment = require('moment');
const { DateSchema: YupDateSchema } = require('yup');

class DateSchema extends YupDateSchema {
  constructor() {
    super({ type: 'date' });

    this.withMutation((schema) => {
      schema.transform((value) => {
        if (!value || !this.isType(value)) {
          return null;
        }
        return value;
      });
    });
  }

  static _typeCheck(value) {
    return moment(value).isValid();
  }
}

module.exports = DateSchema;
