YYYYMMDD Date format on yaml definition - rest

I have a requirement where the request pass date in YYYYMMDD format. Based on swagger documentation, date filed defined under string type. However, it follows RFC 3339, section 5.6, documentation (ex.2018-03-20 as format)
below code doesn't work with yaml.
dateOfBirth:
type: string
minLength: 8
maxLength: 8
format: date
example: 19000101
description: Birth date of the member in YYYYMMDD format.
How to define YAML definition for the date format of YYYYMMDD.

According to documentation for type string you can add regex pattern to define the date format YYYYMMDD :
pattern: '^\d{4}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])$'
Also let me suggest you to update your example date format with a date like 20190317 to easily understand the expected date format.
definitions:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
dateOfBirth:
type: string
minLength: 8
maxLength: 8
format: date
pattern: '^\d{4}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])$'
example: 20190816
description: Birth date of the member in YYYYMMDD format.

Related

SwiftUI return type Error: Cannot convert return expression of type 'Foundation.Date' to return type 'BarCharts.Date'

I am doing a bar chart in my SwiftUI app, however I encountered a problem...
struct ViewMonth: Identifiable {
let id = UUID()
let date: Date
let viewCount: Int
}
I defined several variable types in the above code.
struct Date {
static func from(year: Int, month: Int, day: Int) -> Date {
let components = DateComponents(year: year, month: month, day: day)
return Calendar.current.date(from: components)!
}
}
It seems like I can't convert the expression type 'Foundation.Date' to 'BarCharts.Date'. I don't understand the error message. Please help!
I expect the code will yield no errors.
The error complains about a terminology conflict of your type Date with existing Date in Foundation.
Basically there are three options:
Replace struct Date with extension Date.
Rename your struct Date as something which does not interfere with any type in the Foundation framework.
Declare all instances of your custom type as BarCharts.Date.
I recommend the first option.
There are two types: Foundation.Date is the date type that everyone uses. And I suppose you defined your struct Date inside a class Barcharts, so it is a Barcharts.Date.
The “from” function is declared to return Date - which is a shortcut for Barcharts.Date. Now the error message should make sense. It’s up to you to decide what you wanted it to return.

Epoch to Date during the decode

I have the following json object.
{"date" : 1596304900, "name" : "registration"}
I have the following struct
struct Classroom : Codable{
let date : Int
let name : String
}
As you see in the json object, date is actually epoch time and want to convert it to human readable date during the decode operation. I wonder how it needs to be done during the decode.
Make your date a Date instead of an Int, and use a JSONDecoder with its date decoding strategy set to the epoch decoder:
https://developer.apple.com/documentation/foundation/jsondecoder/datedecodingstrategy/secondssince1970
Everything will happen correctly as if by magic. (In the example you've given, the date will turn out to be August 1, 2020, at 18:01:40 GMT.)
Note that this will not be "human-readable"; it will be a date. You don't store human-readable; you show human readable. You deal with that in the interface only, with a formatter.

How to validate an input string as a valid date in Kotlin?

So my Kotlin app is accepting an input String that should be a date in a certain format:
fun haveFun(dateStr: String){
var formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy")
var formattedDate = dateStr.format(formatter)
println(formattedDate)
}
The issue is that no matter which input string I send, it seems that everything is valid and no error is thrown.
The program is cool with whatever text I sent it: 61-Boby-2019 or even I'm not a date
From Java I'm used to some exception to be thrown or an isValid method, but I didn't find such yet in Kotlin
This should work. instead of using the format() use parse() it will throw exception if it fails and handle that at calling side.
#Throws(ParseException::class)
fun haveFun(dateStr: String) {
var formatter = SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault())
val date = formatter.parse(dateStr)
println(date)
}
it will throw error like below:
java.text.ParseException: Unparseable date: "im not date"
You are using the wrong methods to format your date.
The method format which you are using (dateStr.format(formatter)) is for formatting the input which takes the current string as a format string as stated here:
fun String.format(vararg args: Any?): String Uses this string as a
format string and returns a string obtained by substituting the
specified arguments, using the default locale.
You need to do something else in order to achieve what you are looking for.
var formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy")
formatter.format(LocalDate.now()) //12-Dec-2019

How do I combine 'nil' and an !Included datatype?

I am defining a RAML 1.0 datatype with property that can be 'nil.' The type of that property is an !included datatype which, whilst not giving any errors in the datatype definition causes the root object to throw a "Unresolved reference '!includereference-data.raml' at libraries/types/personal-details.raml (10, 12)'
I've tried converting the DataType to a library to try and implement a "uses" or "types" but that impacts all the other objects that include this data type.
#%RAML 1.0 DataType
properties:
DateOfBirth: datetime
FirstName: string
FamilyName: nil | string
PreferredName: nil | string
PreviousNames: nil | string
Title: !include reference-data.raml
Gender: nil | !include reference-data.raml
The 'Title' property is working as expected, the error is thrown against the Gender property - I actually want both to be nillable.
The datatype to hold nillable properties here is "name_block.raml"
#%RAML 1.0 DataType
uses:
lib: base_types.raml
type: object
properties:
FirstName: lib.string30
MiddleName: lib.nstring30
LastName: lib.string150
The included library (base_types.raml) is defined as
#%RAML 1.0 Library
types:
string30:
type: string
minLength: 0
maxLength: 30
string150:
type: string
minLength: 0
maxLength: 150
nstring30:
type: string | nil
If anyone knows of a better way - It would be great to see it.

Swift HKCategorySample

How do I create an HKCategorySample? I'm following the docs but it errors with
Cannot invoke 'init' with an argument list of type '(type: HKCategoryType, value: Int, startDate: NSDate, endDate: NSDate)'
The docs seem to indicate that those are the correct argument types to pass it, so what is it actually complaining about?
The code I'm using is:
let sample = HKCategorySample(
type: HKCategoryTypeIdentifierSleepAnalysis as HKCategoryType,
value: HKCategoryValueSleepAnalysis.Asleep as Int,
startDate: start,
endDate: end)
where both start and end are NSDates
You have 2 problems in your code:
HKCategoryTypeIdentifierSleepAnalysis is just a identifier String, you must instantiate it with HKObjectType.categoryTypeForIdentifier()
HKCategoryValueSleepAnalysis is enum, you cannot cast it to Int. To extract Int from it, you have to use .rawValue property.
Try:
let sample = HKCategorySample(
type: HKObjectType.categoryTypeForIdentifier(HKCategoryTypeIdentifierSleepAnalysis),
value: HKCategoryValueSleepAnalysis.Asleep.rawValue,
startDate: start,
endDate: end
)