const clone = require('clone');
const tv4 = require('tv4');

class Dot {
  constructor(opt) {
    this.number = null;
    this.hazardClass = null;
    this.packingGroup = null;
    this.shippingName = null;

    if (opt) {
      this.fillObject(clone(opt));
    }
  }

  fillObject(opt) {
    Object.keys(this).forEach((prop) => {
      this[prop] = opt[prop] || null;
    });
  }

  validate() {
    return tv4.validateMultiple(this, Dot.schema());
  }

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'dot.schema.json',
      type: 'object',
      properties: {
        number: { type: ['string', 'null'] },
        hazardClass: { type: ['string', 'null'] },
        packingGroup: { type: ['string', 'null'] },
        shippingName: { type: ['string', 'null'] },
      },
      required: Object.keys(this),
    };
  }
}

module.exports = Dot;
