Skip to content

Commit

Permalink
docs: update to date format
Browse files Browse the repository at this point in the history
  • Loading branch information
JeelRajodiya committed Jun 4, 2024
1 parent bed1e1d commit 0975e7c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 43 deletions.
58 changes: 24 additions & 34 deletions content/01-introduction/06-Defining-Properties-Using-oneOf/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
};
Expand All @@ -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,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,41 @@ 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"] }
]
}
```

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.

<GoodToKnowBox>
you can also use the `not` keyword to ensure that both properties are not present at the same time.
Expand Down

0 comments on commit 0975e7c

Please sign in to comment.