Date
String
class User {
public dob: Date;
}
const date = `2021-06-07T15:12:29.140Z`;
const max = `2021-06-07T15:12:30.140Z`;
const min = `2021-06-07T15:12:28.140Z`;
const dobField = new StringDateField({
deconvert: _date => _date.toISOString(),
max,
min,
});
const USER_META = new ObjectMeta({
builder: User,
fields: {
dob: dobField
}
});
const values = [
{
dob: date,
},
{
dob: `aaacccc`
},
{
dob: 12
},
{
dob: new Date(+(new Date(max)) + 1000).toISOString(),
},
{
dob: new Date(+(new Date(min)) - 1000).toISOString(),
}
];
const result = JTC.convert({
id: `Date Strings`,
meta: new ObjectArrayMeta({ meta: USER_META }),
values,
});
const validation = dobField.validate({ value: new Date(max + 1) });
const log = JTC.log.asString(result.tree);
const [user] = result.converted.all;
const deconverted = JTC.deconvert({ value: user });
console.log(user);
/* { dob: '2021-06-07T15:12:29.140Z' } */
console.log(deconverted);
/* 1 (EXCLUDED) -> {...} | Validation failed for all fields
1 (EXCLUDED) -> dob -> aaacccc | Can't create Date from such value
2 (EXCLUDED) -> {...} | Validation failed for all fields
2 (EXCLUDED) -> dob -> 12 | Expected string, but got number
3 (1) -> dob -> 2021-06-07T15:12:31.140Z | Later than 2021-06-07T15:12:30.140Z
4 (2) -> dob -> 2021-06-07T15:12:27.140Z | Earlier than 2021-06-07T15:12:28.140Z*/
console.log(log);
console.log(validation.errors);
/* true */
console.log(validation.isInvalid);Millis
Seconds
Last updated