Skip to content

Commit

Permalink
fix dynamic objects e.g. JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchoz49 committed Jul 28, 2024
1 parent 537a823 commit b2699e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,16 @@ export const write = async (source, opts = {}) => {
let optionsString
const optionsKeys = Object.keys(options)
const propertyKeys = Object.keys(properties)
// if it has options or the properties are empty meaning it's a json object
if (optionsKeys.length > 0 || propertyKeys.length === 0) {

// this is necessary to allow validate dynamic json objects e.g "metadata: { type: 'object' }"
if (propertyKeys.length === 0 && !optionsKeys.includes('additionalProperties')) {
optionsKeys.push('additionalProperties')
options.additionalProperties = true
}

if (optionsKeys.length > 0) {
optionsString = getWriterString(() => {
w.inlineBlock(() => {
// this is necessary to allow validate dynamic json objects
if (!optionsKeys.includes('additionalProperties')) {
optionsKeys.push('additionalProperties')
options.additionalProperties = true
}

optionsKeys.forEach((optionKey) => {
const value = options[optionKey]
w.write(`'${optionKey}': `)
Expand All @@ -311,7 +311,7 @@ export const write = async (source, opts = {}) => {
}

if (propertyKeys.length === 0) {
w.write(`T.Object({},${optionsString})`)
w.write(`T.Object({}${optionsString ? ',' + optionsString : ''})`)
} else {
w.write('T.Object(')
// TODO: use ref
Expand Down
Loading

0 comments on commit b2699e3

Please sign in to comment.