What encoding and decoding special code needs to be added to the server app when a model includes a Swift Date property? Eg, say you add a Date property to Acronym to track the first time this acronym came into popular use. When I send a request to create an acronym the Vapor app reports [ ERROR ] DecodingError.typeMismatch: POST /api/acronyms Value of type ‘String’ required for key ‘dateOfFirstReportedUse’. (ErrorMiddleware.swift:26)
The Date property will be set at the time of creation of the object. I simply added the (contrived) property to the model as such.
final class Acronym: Codable {
var id: Int?
var short: String
var long: String
var dateOfFirstReportedUse: Date
var userID: UUID
init(short: String, long: String, dateOfFirstReportedUse: Date, userID: UUID) {
self.short = short
self.long = long
self.dateOfFirstReportedUse = dateOfFirstReportedUse
self.userID = userID
}
}