This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
add description and required control #32
Open
adamskrodzki
wants to merge
3
commits into
aspecto-io:master
Choose a base branch
from
adamskrodzki:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,71 @@ | ||
import { ValueType, Schema, SchemaGenOptions } from './types'; | ||
|
||
function createSchemaFor(value: any, options?: SchemaGenOptions): Schema { | ||
function createSchemaFor(value: any, options?: SchemaGenOptions, description? : string): Schema { | ||
switch (typeof value) { | ||
case 'number': | ||
if (Number.isInteger(value)) { | ||
return { type: ValueType.Integer }; | ||
return description ? { type: ValueType.Integer, description } : { type: ValueType.Integer }; | ||
} | ||
return { type: ValueType.Number }; | ||
return description ? { type: ValueType.Number, description } : { type: ValueType.Number }; | ||
case 'boolean': | ||
return { type: ValueType.Boolean }; | ||
return description ? { type: ValueType.Boolean, description } : { type: ValueType.Boolean }; | ||
case 'string': | ||
return { type: ValueType.String }; | ||
return description ? { type: ValueType.String, description } : { type: ValueType.String }; | ||
case 'object': | ||
if (value === null) { | ||
return { type: ValueType.Null }; | ||
return description ? { type: ValueType.Null, description } : { type: ValueType.Null }; | ||
} | ||
if (Array.isArray(value)) { | ||
return createSchemaForArray(value, options); | ||
return createSchemaForArray(value, options, description); | ||
} | ||
return createSchemaForObject(value, options); | ||
return createSchemaForObject(value, options, description); | ||
} | ||
} | ||
|
||
function createSchemaForArray(arr: Array<any>, options?: SchemaGenOptions): Schema { | ||
function createSchemaForArray(arr: Array<any>, options?: SchemaGenOptions, description? : string): Schema { | ||
if (arr.length === 0) { | ||
return { type: ValueType.Array }; | ||
return description ? { type: ValueType.Array, description } : { type: ValueType.Array }; | ||
} | ||
const elementSchemas = arr.map((value) => createSchemaFor(value, options)); | ||
const items = combineSchemas(elementSchemas); | ||
return { type: ValueType.Array, items }; | ||
return description ? { type: ValueType.Array, items, description } : { type: ValueType.Array, items }; | ||
} | ||
|
||
function createSchemaForObject(obj: Object, options?: SchemaGenOptions): Schema { | ||
function createSchemaForObject(obj: Object, options?: SchemaGenOptions, description? : string): Schema { | ||
const keys = Object.keys(obj); | ||
const nonFunctionKeys = keys.filter((key) => typeof obj[key] !== 'function'); | ||
if (keys.length === 0) { | ||
return { | ||
|
||
return description ? { | ||
type: ValueType.Object, | ||
}; | ||
description | ||
} : { type: ValueType.Object }; | ||
} | ||
const properties = Object.entries(obj).reduce((props, [key, val]) => { | ||
props[key] = createSchemaFor(val, options); | ||
let description : string | undefined = undefined; | ||
|
||
if(obj["addDescriptionOfProperty"]){ | ||
description= obj["addDescriptionOfProperty"](key); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. special, reserved function. function takes property name and returns description or |
||
} | ||
|
||
if(typeof val !== 'function'){ | ||
props[key] = createSchemaFor(val, options, description); | ||
} | ||
return props; | ||
}, {}); | ||
|
||
const schema: Schema = { type: ValueType.Object, properties }; | ||
const schema: Schema = description ? { type: ValueType.Object, properties, description } : { type: ValueType.Object, properties }; | ||
|
||
let requiredKeys = nonFunctionKeys; | ||
|
||
if (!options?.noRequired) { | ||
schema.required = keys; | ||
if(obj["excludeFromRequired"]){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. special, reserved function. Use to specify which properties should be excluded from the |
||
const excluded : string[] = obj["excludeFromRequired"](); | ||
requiredKeys = requiredKeys.filter(x => !excluded.includes(x)) | ||
} | ||
schema.required = requiredKeys; | ||
} | ||
|
||
return schema; | ||
} | ||
|
||
|
@@ -231,9 +250,9 @@ function isContainerSchema(schema: Schema): boolean { | |
// FACADE | ||
|
||
export function createSchema(value: any, options?: SchemaGenOptions): Schema { | ||
JSON.stringify(value);//just to catch circular dependencies | ||
if (typeof value === 'undefined') value = null; | ||
const clone = JSON.parse(JSON.stringify(value)); | ||
return createSchemaFor(clone, options); | ||
return createSchemaFor(value, options); | ||
} | ||
|
||
export function mergeSchemas(schemas: Schema[], options?: SchemaGenOptions): Schema { | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
description should be added to schema only if description is not null.