2

I'm building an API with LoopBack 4, and in a model there is a property called "day" which is a Date type (the MySQL column is also type Date).

But I can't post values like "2019-09-09" to it, because it wants something like "2019-09-09T12:41:05.942Z". How can I specify that it has to be a date (without time)?

I'm confused because you can pass "2019-09-09" in query parameters (that are of type date), but not in models.

I currently have the property in the model like this:

@property({
    type: Date,
    required: true,
    mysql: {
        columnName: 'day',
        dataType: 'date',
        dataLength: null,
        dataPrecision: null,
        dataScale: null,
        nullable: 'N',
    },
})
day: Date;

Expected: accept "2019-09-09" as value

Actually: 422: day should match format "date-time"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Why not just post it with the time? MySQL will just ignore it, right? – Heretic Monkey Sep 09 '19 at 12:53
  • I'm rebuilding an old API which accepts the day without the time. And it would be very helpful if the API's don't have much difference so old clients that used the old API, still work with the new one. – Clemens Ertle Sep 09 '19 at 12:57

2 Answers2

3

I had the same problem and I solved it using jsonSchema for specify the format of the request JSON.

In your case, you have to change your code by:

@property({
    type: 'date', // Types in LoopBack4 are not case-sensitive so Date is the same than date
    jsonSchema: { 
      format: 'date', //This can be changed to 'date-time', 'time' or 'date'
    },
    required: true,
    mysql: {
        columnName: 'day',
        dataType: 'date',
        dataLength: null,
        dataPrecision: null,
        dataScale: null,
        nullable: 'N',
    },
})
day: string; // Change this also

0

The type should be changed to 'date' instead of Date. This would allow for a more flexible date schema:

@property({
    type: 'date',
    required: true,
    mysql: {
        columnName: 'day',
        dataType: 'date',
        dataLength: null,
        dataPrecision: null,
        dataScale: null,
        nullable: 'N',
    },
})
day: string;
Rifa Achrinza
  • 1,555
  • 9
  • 19