chrono icon indicating copy to clipboard operation
chrono copied to clipboard

ParsedComponents "assign" function should do some sanity checks

Open mvolz opened this issue 7 years ago • 2 comments

The ParsedComponents "assign" function doesn't appear to check input validity at all and allows you to set objects and other non-valid input as values.

For instance you can do the following:

let c = new chrono.ParsedComponents(null, '1970-01-01');

ParsedComponents {
  knownValues: {},
  impliedValues: 
   { day: 1,
     month: 1,
     year: 1970,
     hour: 12,
     minute: 0,
     second: 0,
     millisecond: 0 } }
c.assign('year', {elephant:'elephant'});

c is now

ParsedComponents {
  knownValues: { year: { elephant: 'elephant' }, timezoneOffset: 0 },
  impliedValues: { day: 1, month: 1, hour: 12, minute: 0, second: 0, millisecond: 0 } }

The reason this is relevant to me is because I'm using assign to set values using third party data. The third party data should be giving valid inputs but you never know! So I was hoping using the assign function (as opposed to setting the values directly into the object) would do this for me :).

.date() doesn't choke on this, but it does default to 2018 even though the previous object had an implied value of 1970: 2018-01-01T12:00:00.000Z. So assign is removing the implied value I assume even though it can't use the incorrect known value and that's where the 2018 is coming from.

mvolz avatar Mar 15 '18 11:03 mvolz

This is very good suggestion, but I wonder what you think would be the best way handle the error. For example, you expect the error to be thrown or simply ignore the assign() operation?

wanasit avatar Jun 02 '18 04:06 wanasit

I don't have a strong opinion either way.

mvolz avatar Jun 04 '18 12:06 mvolz