Skip to content

Commit

Permalink
fix: Rename internal Document option as _directives to resolve confli…
Browse files Browse the repository at this point in the history
…ct with ToString options (fixes #389)
  • Loading branch information
eemeli committed May 14, 2022
1 parent 3ed008a commit a918afa
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/compose/compose-doc.ts
Expand Up @@ -21,7 +21,7 @@ export function composeDoc(
{ offset, start, value, end }: CST.Document,
onError: ComposeErrorHandler
) {
const opts = Object.assign({ directives }, options)
const opts = Object.assign({ _directives: directives }, options)
const doc = new Document(undefined, opts) as Document.Parsed
const ctx: ComposeContext = {
atRoot: true,
Expand Down
2 changes: 1 addition & 1 deletion src/compose/composer.ts
Expand Up @@ -246,7 +246,7 @@ export class Composer {
yield this.doc
this.doc = null
} else if (forceDoc) {
const opts = Object.assign({ directives: this.directives }, this.options)
const opts = Object.assign({ _directives: this.directives }, this.options)
const doc = new Document(undefined, opts) as Document.Parsed
if (this.atDirectives)
this.onError(
Expand Down
6 changes: 3 additions & 3 deletions src/doc/Document.ts
Expand Up @@ -62,7 +62,7 @@ export class Document<T extends Node = Node> {
options: Required<
Omit<
ParseOptions & DocumentOptions,
'lineCounter' | 'directives' | 'version'
'_directives' | 'lineCounter' | 'version'
>
>

Expand Down Expand Up @@ -125,8 +125,8 @@ export class Document<T extends Node = Node> {
)
this.options = opt
let { version } = opt
if (options?.directives) {
this.directives = options.directives.atDocument()
if (options?._directives) {
this.directives = options._directives.atDocument()
if (this.directives.yaml.explicit) version = this.directives.yaml.version
} else this.directives = new Directives({ version })
this.setSchema(version, options)
Expand Down
3 changes: 2 additions & 1 deletion src/options.ts
Expand Up @@ -65,10 +65,11 @@ export type ParseOptions = {

export type DocumentOptions = {
/**
* @internal
* Used internally by Composer. If set and includes an explicit version,
* that overrides the `version` option.
*/
directives?: Directives
_directives?: Directives

/**
* Control the logging level during parsing
Expand Down
98 changes: 98 additions & 0 deletions src/schema/json-schema.ts
@@ -0,0 +1,98 @@
// https://json-schema.org/draft/2020-12/json-schema-core.html
// https://json-schema.org/draft/2020-12/json-schema-validation.html

type JsonSchema =
| boolean
| ArraySchema
| ObjectSchema
| NumberSchema
| StringSchema

type JsonType =
| 'array'
| 'object'
| 'string'
| 'number'
| 'integer'
| 'boolean'
| 'null'

interface CommonSchema {
type?: JsonType | JsonType[]
const?: unknown
enum?: unknown[]
format?: string

// logic/conditionals
allOf?: JsonSchema[]
anyOf?: JsonSchema[]
oneOf?: JsonSchema[]
not?: JsonSchema
if?: JsonSchema
then?: JsonSchema
else?: JsonSchema

// references
$id?: string // uri
$defs?: Record<string, JsonSchema>
$anchor?: string // [A-Za-z_][\w.-]*
$dynamicAnchor?: string
$ref?: string
$dynamicRef?: string

// meta
$schema?: string // uri
$vocabulary?: Record<string, boolean> // key is uri
$comment?: string

// annotations
default?: unknown
deprecated?: boolean
readOnly?: boolean
writeOnly?: boolean
title?: string
description?: string
examples?: unknown[]
}

interface ArraySchema extends CommonSchema {
prefixItems?: JsonSchema[]
items?: JsonSchema
contains?: JsonSchema
unevaluatedItems?: JsonSchema
maxItems?: number // non-negative integer
minItems?: number // non-negative integer
uniqueItems?: boolean
maxContains?: number // non-negative integer
minContains?: number // non-negative integer
}

interface ObjectSchema extends CommonSchema {
properties?: Record<string, JsonSchema>
patternProperties?: Record<string, JsonSchema> // key is regexp
additionalProperties?: JsonSchema
propertyNames?: JsonSchema
unevaluatedProperties?: JsonSchema
maxProperties?: number // non-negative integer
minProperties?: number // non-negative integer
required?: string[]
dependentRequired?: Record<string, string[]>
dependentSchemas?: Record<string, JsonSchema>
}

interface StringSchema extends CommonSchema {
maxLength?: number // non-negative integer
minLength?: number // non-negative integer
patter?: string // regex
contentEncoding?: string
contentMediaType?: string
contentSchema?: JsonSchema
}

interface NumberSchema extends CommonSchema {
multipleOf?: number
maximum?: number
exclusiveMaximum?: number
minimum?: number
exclusiveMinimum?: number
}

0 comments on commit a918afa

Please sign in to comment.