Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: JSDoc
null
values being interpreted as strings, and added test…
… README (#1377)
- Loading branch information
1 parent
465f450
commit 277b89a
Showing
7 changed files
with
253 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Testing | ||
|
||
## Schema Generation | ||
|
||
To add/update a test case for generating a valid schema from a Typescript file: | ||
|
||
- Look in `test/valid-data` for a sample related to your change. If you don't find one, create your own following the naming convention. For example, when adding the new sample `annotation-default`: | ||
- Create folder `test/valid-data/annotation-default` | ||
- Add `main.ts` to that folder with the type sample | ||
- Update the corresponding `main.ts` file with your changes. | ||
- Run `yarn test:update` to compile the JSON schema | ||
- Add a test to `test/valid-data-annotations.test.ts`, matching a similar pattern to the existing tests. | ||
- Run tests via `yarn jest test/valid-data-annotations.test.ts` (this only runs the subset of tests related to schema validation) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export interface MyObject { | ||
/** | ||
* @default {extra: {field:"value"}} | ||
*/ | ||
nestedField: MyNestedObject; | ||
/** | ||
* @default 10 | ||
*/ | ||
numberField: number; | ||
/** | ||
* @default "hello" | ||
*/ | ||
stringField: string; | ||
/** | ||
* @default true | ||
*/ | ||
booleanField?: boolean; | ||
/** | ||
* @default null | ||
*/ | ||
nullField: null; | ||
/** | ||
* @default [{ numberField2: 10, stringField2: "yes", anyField: null }] | ||
*/ | ||
arrayField: Array<{ numberField2: number; stringField2: string; anyField?: any }>; | ||
} | ||
|
||
/** | ||
* @default {} | ||
*/ | ||
export type MyNestedObject = Record<string, any>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { MyObject } from "./main.js"; | ||
|
||
export const validSamples: MyObject[] = [ | ||
{ | ||
nullField: null, | ||
numberField: 100, | ||
stringField: "goodbye", | ||
arrayField: [], | ||
booleanField: false, | ||
nestedField: {}, | ||
}, | ||
]; | ||
|
||
/** | ||
* Samples that should be *invalid* if | ||
* AJV is not using `useDefaults: true`, | ||
* and otherwise valid. The resulting | ||
* mutated object should be the same | ||
* in all cases. | ||
*/ | ||
export const invalidSamplesUnlessDefaults: any[] = [ | ||
{ | ||
nullField: null, | ||
numberField: 10, | ||
stringField: "hello", | ||
}, | ||
{}, | ||
]; | ||
|
||
/** | ||
* The resulting data structure after | ||
* `useDefaults` is used with the | ||
* {@link invalidSamples} entries. | ||
* | ||
* We aren't testing AJV's behavior here. | ||
* We're assuming that if AJV populates | ||
* defaults, and those defaults are of | ||
* the expected values, then this project | ||
* must be working correctly. | ||
*/ | ||
export const expectedAfterDefaults: MyObject = { | ||
nullField: null, | ||
numberField: 10, | ||
stringField: "hello", | ||
arrayField: [{ numberField2: 10, stringField2: "yes", anyField: null }], | ||
booleanField: true, | ||
nestedField: { extra: { field: "value" } }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"$ref": "#/definitions/MyObject", | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"MyNestedObject": { | ||
"default": {}, | ||
"type": "object" | ||
}, | ||
"MyObject": { | ||
"additionalProperties": false, | ||
"properties": { | ||
"arrayField": { | ||
"default": [ | ||
{ | ||
"anyField": null, | ||
"numberField2": 10, | ||
"stringField2": "yes" | ||
} | ||
], | ||
"items": { | ||
"additionalProperties": false, | ||
"properties": { | ||
"anyField": {}, | ||
"numberField2": { | ||
"type": "number" | ||
}, | ||
"stringField2": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"numberField2", | ||
"stringField2" | ||
], | ||
"type": "object" | ||
}, | ||
"type": "array" | ||
}, | ||
"booleanField": { | ||
"default": true, | ||
"type": "boolean" | ||
}, | ||
"nestedField": { | ||
"$ref": "#/definitions/MyNestedObject", | ||
"default": { | ||
"extra": { | ||
"field": "value" | ||
} | ||
} | ||
}, | ||
"nullField": { | ||
"default": null, | ||
"type": "null" | ||
}, | ||
"numberField": { | ||
"default": 10, | ||
"type": "number" | ||
}, | ||
"stringField": { | ||
"default": "hello", | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"nestedField", | ||
"numberField", | ||
"stringField", | ||
"nullField", | ||
"arrayField" | ||
], | ||
"type": "object" | ||
} | ||
} | ||
} |