io-ts-from-json-schema
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -389,3 +389,3 @@ #!/usr/bin/env node | ||
} | ||
function fromRef(refObject, name) { | ||
function fromRef(refObject) { | ||
var refString = refObject.$ref, _comment = refObject.$comment, extra = __rest(refObject, ["$ref", "$comment"]); | ||
@@ -405,5 +405,2 @@ if (Object.keys(extra).length) { | ||
if (ref.filePath === '') { | ||
if (name) { | ||
exps.add("export type " + name + "C = " + ref.variableName + "C;"); | ||
} | ||
return gen.customCombinator(ref.variableName, ref.variableName, [ref.variableName]); | ||
@@ -414,5 +411,2 @@ } | ||
imps.add("import * as " + importName + " from '" + importPath + "';"); | ||
if (name) { | ||
exps.add("export type " + name + "C = " + importName + "." + ref.variableName + "C;"); | ||
} | ||
var variableRef = importName + "." + ref.variableName; | ||
@@ -422,6 +416,9 @@ return gen.customCombinator(variableRef, variableRef, [importName]); | ||
function isSupported(feature, isRoot) { | ||
if (isRoot && supportedAtRoot.includes(feature)) { | ||
if (supportedEverywhere.includes(feature)) { | ||
return true; | ||
} | ||
return supportedEverywhere.includes(feature); | ||
if (isRoot) { | ||
return supportedAtRoot.includes(feature); | ||
} | ||
return supportedOutsideRoot.includes(feature); | ||
} | ||
@@ -569,3 +566,6 @@ function fromType(schema) { | ||
// accept nothing | ||
return error('Not sure how to deal with a schema that matches nothing'); | ||
return gen.unionCombinator([ | ||
gen.literalCombinator(true), | ||
gen.literalCombinator(false), | ||
]); | ||
} | ||
@@ -642,16 +642,17 @@ } | ||
var name = capitalize(k); | ||
var examples = extractExamples(scem); | ||
var defaultValue = extractDefaultValue(scem); | ||
if (typeof scem === 'boolean') { | ||
var title = undefined; | ||
var description = undefined; | ||
return [ | ||
{ | ||
meta: { | ||
title: title, | ||
description: description, | ||
examples: examples, | ||
defaultValue: defaultValue, | ||
title: undefined, | ||
description: undefined, | ||
examples: [], | ||
defaultValue: undefined, | ||
}, | ||
dec: gen.typeDeclaration(name, error("Any and never types are not supported by convert.ts"), true), | ||
dec: gen.typeDeclaration(name, gen.brandCombinator(scem | ||
? gen.unknownType | ||
: gen.unionCombinator([ | ||
gen.literalCombinator(true), | ||
gen.literalCombinator(false), | ||
]), function (_x) { return String(scem); }, name), true), | ||
}, | ||
@@ -662,13 +663,11 @@ ]; | ||
// ref's do not have meta data | ||
var title = undefined; | ||
var description = undefined; | ||
return [ | ||
{ | ||
meta: { | ||
title: title, | ||
description: description, | ||
examples: examples, | ||
defaultValue: defaultValue, | ||
title: undefined, | ||
description: undefined, | ||
examples: [], | ||
defaultValue: undefined, | ||
}, | ||
dec: gen.typeDeclaration(name, fromRef(scem, name), true), | ||
dec: gen.typeDeclaration(name, gen.brandCombinator(fromRef(scem), function (_x) { return String(true); }, name), true), | ||
}, | ||
@@ -682,4 +681,4 @@ ]; | ||
description: scem.description, | ||
examples: examples, | ||
defaultValue: defaultValue, | ||
examples: extractExamples(scem), | ||
defaultValue: extractDefaultValue(scem), | ||
}, | ||
@@ -691,8 +690,10 @@ dec: gen.typeDeclaration(name, gen.brandCombinator(fromSchema(scem, true), function (x) { return generateChecks(x, scem); }, name), true), | ||
} | ||
function fromNonRefRoot(schema) { | ||
function fromRoot(root) { | ||
// root schema info is printed in the beginning of the file | ||
var title = defaultExport; | ||
var description = 'The default export. More information at the top.'; | ||
var examples = extractExamples(schema); | ||
var defaultValue = extractDefaultValue(schema); | ||
var examples = extractExamples(root); | ||
var defaultValue = extractDefaultValue(root); | ||
imps.add("import * as t from 'io-ts';"); | ||
exps.add("export default " + defaultExport + ";"); | ||
return [ | ||
@@ -706,33 +707,8 @@ { | ||
}, | ||
dec: gen.typeDeclaration(defaultExport, gen.brandCombinator(fromSchema(schema, true), function (x) { return generateChecks(x, schema); }, defaultExport), true), | ||
dec: gen.typeDeclaration(defaultExport, gen.brandCombinator(isRefObject(root) | ||
? error('schema root can not be a $ref object') | ||
: fromSchema(root, true), function (x) { return generateChecks(x, root); }, defaultExport), true), | ||
}, | ||
]; | ||
} | ||
function fromRoot(root) { | ||
// root schema info is printed in the beginning of the file | ||
var title = defaultExport; | ||
var description = 'The default export. More information at the top.'; | ||
var examples = extractExamples(root); | ||
var defaultValue = extractDefaultValue(root); | ||
if (isRefObject(root)) { | ||
exps.add("export default " + defaultExport + ";"); | ||
return [ | ||
{ | ||
meta: { | ||
title: title, | ||
description: description, | ||
examples: examples, | ||
defaultValue: defaultValue, | ||
}, | ||
dec: gen.typeDeclaration(defaultExport, fromRef(root, defaultExport), true), | ||
}, | ||
]; | ||
} | ||
var items = fromNonRefRoot(root); | ||
if (items.length > 0) { | ||
imps.add("import * as t from 'io-ts';"); | ||
exps.add("export default " + defaultExport + ";"); | ||
} | ||
return items; | ||
} | ||
function fromFile(schema) { | ||
@@ -792,3 +768,3 @@ var namedDefs = fromDefinitions(schema.definitions); | ||
} | ||
var imports, definedHelper, Defined, supportedEverywhere, supportedAtRoot, documentBase, defaultExport, imps, helpers, exps, ErrorCode, OK, returnCode, inputs, defs, defs_1, defs_1_1, def, typeName, title, description, examples, defaultValue, staticType, runtimeType, examplesName, defaultName, e_1_1; | ||
var imports, definedHelper, Defined, supportedEverywhere, supportedAtRoot, supportedOutsideRoot, documentBase, defaultExport, imps, helpers, exps, ErrorCode, OK, returnCode, inputs, defs, defs_1, defs_1_1, def, typeName, title, description, examples, defaultValue, staticType, runtimeType, examplesName, defaultName, e_1_1; | ||
var e_1, _a; | ||
@@ -802,3 +778,2 @@ return __generator(this, function (_b) { | ||
supportedEverywhere = [ | ||
'$ref', | ||
'$id', | ||
@@ -837,2 +812,3 @@ 'title', | ||
]; | ||
supportedOutsideRoot = ['$ref']; | ||
documentBase = (function () { | ||
@@ -839,0 +815,0 @@ var _a = __read(args.documentURI.split('/').reverse()), reversePath = _a.slice(1); |
{ | ||
"name": "io-ts-from-json-schema", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "Iotsfjs is a static code generation utility used for converting json schema files into static TypeScript types and io-ts runtime validators.", | ||
@@ -42,3 +42,3 @@ "main": "lib/cli.js", | ||
"jest": "^26.4.2", | ||
"maas-schemas-git-develop": "git://github.com/maasglobal/maas-schemas.git#develop", | ||
"maas-schemas-git-develop": "git://github.com/cyberixae/maas-schemas.git#fix_broken_ref_objects", | ||
"monocle-ts": "^2.1.0", | ||
@@ -45,0 +45,0 @@ "prettier": "^2.0.5", |
59992
1351