From 0975e7c875f5d5795e1a5a39cc21efd10fdd72cb Mon Sep 17 00:00:00 2001 From: JeelRajodiya Date: Tue, 4 Jun 2024 23:03:10 +0530 Subject: [PATCH] docs: update to date format --- .../code.ts | 58 ++++++++----------- .../instructions.mdx | 22 ++++--- 2 files changed, 37 insertions(+), 43 deletions(-) diff --git a/content/01-introduction/06-Defining-Properties-Using-oneOf/code.ts b/content/01-introduction/06-Defining-Properties-Using-oneOf/code.ts index 59973d5..b99d5ff 100644 --- a/content/01-introduction/06-Defining-Properties-Using-oneOf/code.ts +++ b/content/01-introduction/06-Defining-Properties-Using-oneOf/code.ts @@ -10,24 +10,7 @@ const code: any = { maximum: 60, }, dateOfBirth: { - type: "object", - properties: { - year: { - type: "integer", - minimum: 1964, - maximum: 2024, - }, - month: { - type: "integer", - minimum: 1, - maximum: 12, - }, - day: { - type: "integer", - minimum: 1, - maximum: 31, - }, - }, + type: "string", }, }, }; @@ -41,41 +24,48 @@ solution.oneOf = [ required: ["dateOfBirth"], }, ]; +solution.properties.dateOfBirth.format = "date"; const testCases: any[] = [ { input: { - name: "John Doe", - age: 25, + name: "person", + age: 23, }, expected: true, }, { input: { - name: "John Doe", - dateOfBirth: { - year: 1998, - month: 5, - day: 12, - }, + name: null, + age: 23, + }, + expected: false, + }, + { + input: { + name: "person", + dateOfBirth: "1998-05-12", }, expected: true, }, { input: { - name: "John Doe", + name: "person", + dateOfBirth: "1st January 2025", + }, + expected: false, + }, + { + input: { + name: "person", + age: 23, + dateOfBirth: "1998-05-12", }, expected: false, }, { input: { - name: "John Doe", - age: 25, - dateOfBirth: { - year: 1998, - month: 5, - day: 12, - }, + name: "person", }, expected: false, }, diff --git a/content/01-introduction/06-Defining-Properties-Using-oneOf/instructions.mdx b/content/01-introduction/06-Defining-Properties-Using-oneOf/instructions.mdx index 111c372..66d0a90 100644 --- a/content/01-introduction/06-Defining-Properties-Using-oneOf/instructions.mdx +++ b/content/01-introduction/06-Defining-Properties-Using-oneOf/instructions.mdx @@ -13,29 +13,33 @@ In the previous step, we had `age` and `dateOfBirth` properties in the schema. H - both `age` and `dateOfBirth` should not be present at the same time. - if none of them is present, the document should be invalid. -```json +```json highlightLineStart={4} { "name": "John Doe", "age": 25, - "dateOfBirth": { - "day": 1, - "month": 1, - "year": 1995 - } + "dateOfBirth": "1999-01-01" } ``` +notice that we are using `dateOfBirth` as a string in the JSON document. we will also use inbuilt *JSON Schema* formats to validate the date format. + ## Schema Definition -`oneOf` keyword is used to define properties with multiple types. It ensures that only one of the specified schemas is valid. +`oneOf` keyword is used to define properties with multiple types. It ensures that **exactly one** of the specified *subschemas* is valid. We will combine using `oneOf` and `required`, to ensure that only one of the properties is present in the JSON document. +add `format: "date"` to the `dateOfBirth` property to validate the date format. +this will ensure that the date is in the format `YYYY-MM-DD`. + ```json { "type": "object", - ... + "properties": { + ... + "dateOfBirth": { "type": "string", "format": "date" } + }, "oneOf": [ { "required": ["age"] }, { "required": ["dateOfBirth"] } @@ -43,7 +47,7 @@ We will combine using `oneOf` and `required`, to ensure that only one of the pro } ``` -Now, Let's try to add the `oneOf` keyword to the schema given in the right side editor. +Now, Let's try to add the `format` and `oneOf` keyword to the schema given in the right side editor. you can also use the `not` keyword to ensure that both properties are not present at the same time.