Schema util

schema-util (node operation)

It's hard to work with them if you don't know what means what. In this operation I'll summarize everything and give examples.

Size: 395 LOC, 2291 data characters,

Imported dependencies:

  • From Core Libraries: none
  • From Packages: JSONSchema7Definition, JSONSchema7, JSONSchema7
  • From Operations: SimplifiedSchema, referenceParameterNames, referencePluralParameterNames, lowerCaseArray, pascalCase, makeArray, notEmpty, TsInterface, SimplifiedSchema, log, notEmpty, SimplifiedSchema, SimplifiedSchemaProperty, SimplifiedSchemaItem

Functions

getReferencableModels

Max. indexation depth: 3,

based on the object properties in SimplifiedSchema, returns the model names that can be referenced

Returns: array

  • null: object

Arguments

simplifiedSchema (optional): object

JSONSchema7 derivative that has the following capabilities and and characteristics...

- does not include objects in objects that are also referenced to using xxxSlug or xxxId
- recursively finds the references and expands them, unless the references are circular
- easier to read
- has all the information we need
- is able to generate an object with values in the exact format the function needs it
- is able to easily generate a form

Properties:

NameTypeDescription
typestring
description (optional)string
circularRefName (optional)stringsometimes we still need to reference to another schema because this thing is recursive. In that case the ref name will be here
enum (optional)arrayin case of enums this could appear... mostly strings, but e.g. numbers can also be an enum I think
properties (optional)arrayin case of object, this will always appear
items (optional)arrayin case of arrays, this will always appear

getReferenceParameterInfo

Max. indexation depth: 2,

Takes a parameterName and returns information about it according to the convention {descriptorName}_{modelName}{referenceKey} where:

  • descriptorName with the suffixing underscore is optional
  • referenceKey can be slug, index, or id (or there plural variants)
  • modelName should refer to a database model

Returns: object

Properties:

NameTypeDescription
descriptor (optional)string
keyInModel (optional)string
interfaceName (optional)string
isReferenceMultipleParameterboolean
isReferenceSingleParameterboolean
isReferenceParameterboolean

Arguments

parameterName: string

makeSingular

Max. indexation depth: 2,

makes a word singular (only english)

e.g.:

  • indexes becomes index
  • words becomes word

NB: not well tested

Returns: unknown

Arguments

word: string

getSchemaItems

Max. indexation depth: 1,

==========

Returns: unknown

Arguments

schema (optional): object

Properties:

NameTypeDescription
$id (optional)string
$ref (optional)string
$schema (optional)stringMeta schema<br /><br />Recommended values:<br />- 'http://json-schema.org/schema#'<br />- 'http://json-schema.org/hyper-schema#'<br />- 'http://json-schema.org/draft-07/schema#'<br />- 'http://json-schema.org/draft-07/hyper-schema#'
$comment (optional)string
$defs (optional)object
type (optional)object
enum (optional)array
const (optional)objectPrimitive type
multipleOf (optional)number
maximum (optional)number
exclusiveMaximum (optional)number
minimum (optional)number
exclusiveMinimum (optional)number
maxLength (optional)number
minLength (optional)number
pattern (optional)string
items (optional)object
additionalItems (optional)objectJSON Schema v7
maxItems (optional)number
minItems (optional)number
uniqueItems (optional)boolean
contains (optional)object
maxProperties (optional)number
minProperties (optional)number
required (optional)array
properties (optional)object
patternProperties (optional)object
additionalProperties (optional)objectJSON Schema v7
dependencies (optional)object
propertyNames (optional)objectJSON Schema v7
if (optional)objectJSON Schema v7
then (optional)objectJSON Schema v7
else (optional)objectJSON Schema v7
allOf (optional)array
anyOf (optional)array
oneOf (optional)array
not (optional)objectJSON Schema v7
format (optional)string
contentMediaType (optional)string
contentEncoding (optional)string
definitions (optional)object
title (optional)string
description (optional)string
default (optional)objectPrimitive type
readOnly (optional)boolean
writeOnly (optional)boolean
examples (optional)objectPrimitive type

getSchema

Max. indexation depth: 1,

parses a JSONSchema7Definition to JSONSchema7|undefined so we can use it

Returns: object

Properties:

NameTypeDescription

Arguments

maybeSchema (optional): object

JSON Schema v7

Properties:

NameTypeDescription

Max. indexation depth: 1,

gets the $ref from a schema and parses the interface name from it

Returns: unknown

Arguments

ref (optional): string

getProperties

Max. indexation depth: 6,

Gets all the properties of a schema

Returns: array

  • null: object

Arguments

schema (optional): object

Properties:

NameTypeDescription
$id (optional)string
$ref (optional)string
$schema (optional)stringMeta schema<br /><br />Recommended values:<br />- 'http://json-schema.org/schema#'<br />- 'http://json-schema.org/hyper-schema#'<br />- 'http://json-schema.org/draft-07/schema#'<br />- 'http://json-schema.org/draft-07/hyper-schema#'
$comment (optional)string
$defs (optional)object
type (optional)object
enum (optional)array
const (optional)objectPrimitive type
multipleOf (optional)number
maximum (optional)number
exclusiveMaximum (optional)number
minimum (optional)number
exclusiveMinimum (optional)number
maxLength (optional)number
minLength (optional)number
pattern (optional)string
items (optional)object
additionalItems (optional)objectJSON Schema v7
maxItems (optional)number
minItems (optional)number
uniqueItems (optional)boolean
contains (optional)object
maxProperties (optional)number
minProperties (optional)number
required (optional)array
properties (optional)object
patternProperties (optional)object
additionalProperties (optional)objectJSON Schema v7
dependencies (optional)object
propertyNames (optional)objectJSON Schema v7
if (optional)objectJSON Schema v7
then (optional)objectJSON Schema v7
else (optional)objectJSON Schema v7
allOf (optional)array
anyOf (optional)array
oneOf (optional)array
not (optional)objectJSON Schema v7
format (optional)string
contentMediaType (optional)string
contentEncoding (optional)string
definitions (optional)object
title (optional)string
description (optional)string
default (optional)objectPrimitive type
readOnly (optional)boolean
writeOnly (optional)boolean
examples (optional)objectPrimitive type

simplifiedSchemaToTypeDefinitionString

Max. indexation depth: 4,

Converts a simplifiedSchema definition back into a type interface string

With this, types can be generated in different ways

Returns: unknown

Arguments

simplifiedSchema (optional): object

JSONSchema7 derivative that has the following capabilities and and characteristics...

- does not include objects in objects that are also referenced to using xxxSlug or xxxId
- recursively finds the references and expands them, unless the references are circular
- easier to read
- has all the information we need
- is able to generate an object with values in the exact format the function needs it
- is able to easily generate a form

Properties:

NameTypeDescription
typestring
description (optional)string
circularRefName (optional)stringsometimes we still need to reference to another schema because this thing is recursive. In that case the ref name will be here
enum (optional)arrayin case of enums this could appear... mostly strings, but e.g. numbers can also be an enum I think
properties (optional)arrayin case of object, this will always appear
items (optional)arrayin case of arrays, this will always appear

simplifySchema

Max. indexation depth: 5,

return a SimplifiedSchema by giving the JSONSchema7 schema, its name and a list of possible references in the JSONSchema

Returns: unknown

Arguments

name: string

The name of the type interface, (this could be used as $ref).

schema: object

Properties:

NameTypeDescription
$id (optional)string
$ref (optional)string
$schema (optional)stringMeta schema<br /><br />Recommended values:<br />- 'http://json-schema.org/schema#'<br />- 'http://json-schema.org/hyper-schema#'<br />- 'http://json-schema.org/draft-07/schema#'<br />- 'http://json-schema.org/draft-07/hyper-schema#'
$comment (optional)string
$defs (optional)object
type (optional)object
enum (optional)array
const (optional)objectPrimitive type
multipleOf (optional)number
maximum (optional)number
exclusiveMaximum (optional)number
minimum (optional)number
exclusiveMinimum (optional)number
maxLength (optional)number
minLength (optional)number
pattern (optional)string
items (optional)object
additionalItems (optional)objectJSON Schema v7
maxItems (optional)number
minItems (optional)number
uniqueItems (optional)boolean
contains (optional)object
maxProperties (optional)number
minProperties (optional)number
required (optional)array
properties (optional)object
patternProperties (optional)object
additionalProperties (optional)objectJSON Schema v7
dependencies (optional)object
propertyNames (optional)objectJSON Schema v7
if (optional)objectJSON Schema v7
then (optional)objectJSON Schema v7
else (optional)objectJSON Schema v7
allOf (optional)array
anyOf (optional)array
oneOf (optional)array
not (optional)objectJSON Schema v7
format (optional)string
contentMediaType (optional)string
contentEncoding (optional)string
definitions (optional)object
title (optional)string
description (optional)string
default (optional)objectPrimitive type
readOnly (optional)boolean
writeOnly (optional)boolean
examples (optional)objectPrimitive type

possibleRefs: array

  • null: object
The array of other schemas found when crawling file this schema was found in. this also includes all refs to other type interfaces in all schemas in that file

rootStack: array

  • null: string
This function is recursive. If we find any reference to another schema, we will add the name of the current schema to the rootStack and explore that schema.