-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multiple schema distinction in validation (#410)
* feat: add multiple schema distinction in validation * chore: multiple schema distinction - add new end line - modify warning message consts Co-authored-by: Petr Spacek <[email protected]>
- Loading branch information
Showing
13 changed files
with
371 additions
and
22 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
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
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,37 @@ | ||
import { JSONSchema } from '../jsonSchema'; | ||
|
||
export function getSchemaTypeName(schema: JSONSchema): string { | ||
if (schema.$id) { | ||
const type = getSchemaRefTypeTitle(schema.$id); | ||
return type; | ||
} | ||
if (schema.$ref || schema._$ref) { | ||
const type = getSchemaRefTypeTitle(schema.$ref || schema._$ref); | ||
return type; | ||
} | ||
const typeStr = schema.title || (Array.isArray(schema.type) ? schema.type.join(' | ') : schema.type); //object | ||
return typeStr; | ||
} | ||
|
||
/** | ||
* Get type name from reference url | ||
* @param $ref reference to the same file OR to the another component OR to the section in another component: | ||
* `schema-name.schema.json` -> schema-name | ||
* `custom-scheme://shared-schema.json#/definitions/SomeType` -> SomeType | ||
* `custom-scheme://schema-name.schema.json` -> schema-name | ||
* `shared-schema.schema.json#/definitions/SomeType` -> SomeType | ||
* `file:///Users/user/Documents/project/schemas/schema-name.schema.json` -> schema-name | ||
* `#/definitions/SomeType` -> SomeType | ||
* `#/definitions/io.k8s.api.apps.v1.DaemonSetSpec` => io.k8s.api.apps.v1.DaemonSetSpec | ||
* `file:///default_schema_id.yaml` => default_schema_id.yaml | ||
* test: https://regex101.com/r/ZpuXxk/1 | ||
*/ | ||
export function getSchemaRefTypeTitle($ref: string): string { | ||
const match = $ref.match(/^(?:.*\/)?(.*?)(?:\.schema\.json)?$/); | ||
let type = !!match && match[1]; | ||
if (!type) { | ||
type = 'typeNotFound'; | ||
console.error(`$ref (${$ref}) not parsed properly`); | ||
} | ||
return type; | ||
} |
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,79 @@ | ||
{ | ||
"sharedSchema": { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"type1": { | ||
"properties": { | ||
"objA": { | ||
"type": "object" | ||
}, | ||
"propA": { | ||
"type": "string" | ||
}, | ||
"constA": { | ||
"type": "string", | ||
"const": "constForType1" | ||
} | ||
}, | ||
"required": [ | ||
"objA", | ||
"propA", | ||
"constA" | ||
], | ||
"type": "object" | ||
} | ||
} | ||
}, | ||
"schema": { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"type2": { | ||
"properties": { | ||
"obj2": { | ||
"type": "object" | ||
} | ||
}, | ||
"required": [ | ||
"obj2" | ||
], | ||
"type": "object" | ||
}, | ||
"type3": { | ||
"properties": { | ||
"objA": { | ||
"type": "object" | ||
}, | ||
"propA": { | ||
"type": "string" | ||
}, | ||
"constA": { | ||
"type": "string", | ||
"const": "constForType3" | ||
} | ||
}, | ||
"required": [ | ||
"objA", | ||
"propA", | ||
"constA" | ||
], | ||
"type": "object" | ||
} | ||
}, | ||
"properties": { | ||
"test_anyOf_objects": { | ||
"anyOf": [ | ||
{ | ||
"$ref": "sharedSchema.json#/definitions/type1" | ||
}, | ||
{ | ||
"$ref": "#/definitions/type2" | ||
}, | ||
{ | ||
"$ref": "#/definitions/type3" | ||
} | ||
] | ||
} | ||
}, | ||
"type": "object" | ||
} | ||
} |
Oops, something went wrong.