sylvainpolletvillard / ObjectModel

Strong Dynamically Typed Object Modeling for JavaScript
http://objectmodel.js.org
MIT License
467 stars 30 forks source link

pre/post process some datas ? #169

Closed gordielachance closed 1 year ago

gordielachance commented 1 year ago

Is it possible to process some date before or after the validation is done ?

function isDateString(value){
  // parse the string into a date object
  const date = new Date(value);

  // if the parsing failed or the resulting date is invalid, throw an error
  return  !isNaN(date.getTime())
}

export const DateStringModel = BasicModel(String).assert(isDateString);

export const Playlist = new ObjectModel({
    //...
    date: [DateStringModel],
    //...
});

So the validation checks that the data is a valid date string. But them, how can I cast this value to a Date ?

sylvainpolletvillard commented 1 year ago

I'm not sure what you expect here, you can do this pre/post process with your own logic:

YourModel(preprocess(data))
postprocess(YourModel(data))
sylvainpolletvillard commented 1 year ago

closed after inactivity