io-ts-from-json-schema
Advanced tools
Comparing version 0.0.14 to 0.0.15
#!/usr/bin/env node | ||
/// <reference types="node" /> | ||
import { JSONSchema7 } from 'json-schema'; | ||
import * as stream from 'stream'; | ||
import { JSONSchema7 } from 'json-schema'; | ||
export declare type Args = { | ||
@@ -6,0 +6,0 @@ import: Array<string>; |
@@ -74,5 +74,5 @@ #!/usr/bin/env node | ||
exports.iotsfjs = void 0; | ||
var path = require("path"); | ||
var crypto = require("crypto"); | ||
var gen = require("io-ts-codegen"); | ||
var path = require("path"); | ||
var printc_1 = require("./codegen/printc"); | ||
@@ -155,14 +155,9 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ | ||
} | ||
function notImplemented(pre, item, post, fatal) { | ||
if (fatal === void 0) { fatal = false; } | ||
function notImplemented(item, kind) { | ||
var isOutsideRoot = supportedAtRoot.includes(item); | ||
var where = isOutsideRoot ? 'outside top-level definitions' : ''; | ||
var message = [pre, item, post, 'not supported', where] | ||
var message = [item, kind, 'not supported', where] | ||
.filter(function (s) { return s.length > 0; }) | ||
.join(' '); | ||
if (fatal !== true && isOutsideRoot) { | ||
warning(message); | ||
return null; | ||
} | ||
return error(message); | ||
warning(message); | ||
} | ||
@@ -197,43 +192,50 @@ function parseRef(ref) { | ||
} | ||
function checkPattern(x, pattern) { | ||
function checkFormat(jx, format) { | ||
if (format === 'ipv4') { | ||
return "( typeof " + jx + " !== 'string' || ((octets) => octets.length === 4 && octets.map(Number).every((octet) => Number.isInteger(octet) && octet >= 0x00 && octet <= 0xff))(" + jx + ".split('.')) )"; | ||
} | ||
notImplemented(format, 'format'); | ||
return String(true); | ||
} | ||
function checkPattern(jx, pattern) { | ||
var stringLiteral = JSON.stringify(pattern); | ||
return "( typeof x !== 'string' || " + x + ".match(RegExp(" + stringLiteral + ")) !== null )"; | ||
return "( typeof " + jx + " !== 'string' || " + jx + ".match(RegExp(" + stringLiteral + ")) !== null )"; | ||
} | ||
function checkRegexp(x, regexp) { | ||
function checkRegexp(jx, regexp) { | ||
var _a = getRegexpObject(regexp), pattern = _a.pattern, flags = _a.flags; | ||
var patternLiteral = JSON.stringify(pattern); | ||
var flagsLiteral = JSON.stringify(flags); | ||
return "( typeof x !== 'string' || " + x + ".match(RegExp(" + patternLiteral + ", " + flagsLiteral + ")) !== null )"; | ||
return "( typeof " + jx + " !== 'string' || " + jx + ".match(RegExp(" + patternLiteral + ", " + flagsLiteral + ")) !== null )"; | ||
} | ||
function checkMinLength(x, minLength) { | ||
return "( typeof x !== 'string' || " + x + ".length >= " + minLength + " )"; | ||
function checkMinLength(jx, minLength) { | ||
return "( typeof " + jx + " !== 'string' || " + jx + ".length >= " + minLength + " )"; | ||
} | ||
function checkMaxLength(x, maxLength) { | ||
return "( typeof x !== 'string' || " + x + ".length <= " + maxLength + " )"; | ||
function checkMaxLength(jx, maxLength) { | ||
return "( typeof " + jx + " !== 'string' || " + jx + ".length <= " + maxLength + " )"; | ||
} | ||
function checkMinimum(x, minimum) { | ||
return "( typeof x !== 'number' || " + x + " >= " + minimum + " )"; | ||
function checkMinimum(jx, minimum) { | ||
return "( typeof " + jx + " !== 'number' || " + jx + " >= " + minimum + " )"; | ||
} | ||
function checkMaximum(x, maximum) { | ||
return "( typeof x !== 'number' || " + x + " <= " + maximum + " )"; | ||
function checkMaximum(jx, maximum) { | ||
return "( typeof " + jx + " !== 'number' || " + jx + " <= " + maximum + " )"; | ||
} | ||
function checkMultipleOf(x, divisor) { | ||
return "( typeof x !== 'number' || " + x + " % " + divisor + " === 0 )"; | ||
function checkMultipleOf(jx, divisor) { | ||
return "( typeof " + jx + " !== 'number' || " + jx + " % " + divisor + " === 0 )"; | ||
} | ||
function checkInteger(x) { | ||
return "( Number.isInteger(" + x + ") )"; | ||
function checkInteger(jx) { | ||
return "( Number.isInteger(" + jx + ") )"; | ||
} | ||
function checkMinItems(x, minItems) { | ||
return "( Array.isArray(x) === false || " + x + ".length >= " + minItems + " )"; | ||
function checkMinItems(jx, minItems) { | ||
return "( Array.isArray(" + jx + ") === false || " + jx + ".length >= " + minItems + " )"; | ||
} | ||
function checkMaxItems(x, maxItems) { | ||
return "( Array.isArray(x) === false || " + x + ".length <= " + maxItems + " )"; | ||
function checkMaxItems(jx, maxItems) { | ||
return "( Array.isArray(" + jx + ") === false || " + jx + ".length <= " + maxItems + " )"; | ||
} | ||
function checkUniqueItems(x) { | ||
return "( Array.isArray(x) === false || " + x + ".length === [...new Set(x)].length )"; | ||
function checkUniqueItems(jx) { | ||
return "( Array.isArray(" + jx + ") === false || " + jx + ".length === [...new Set(" + jx + ")].length )"; | ||
} | ||
function generateChecks(x, schema) { | ||
var checks = __spread((schema.pattern ? [checkPattern(x, schema.pattern)] : []), (schema.regexp | ||
? [checkRegexp(x, schema.regexp)] | ||
: []), (schema.minLength ? [checkMinLength(x, schema.minLength)] : []), (schema.maxLength ? [checkMaxLength(x, schema.maxLength)] : []), (schema.minimum ? [checkMinimum(x, schema.minimum)] : []), (schema.maximum ? [checkMaximum(x, schema.maximum)] : []), (schema.multipleOf ? [checkMultipleOf(x, schema.multipleOf)] : []), (schema.type === 'integer' ? [checkInteger(x)] : []), (schema.minItems ? [checkMinItems(x, schema.minItems)] : []), (schema.maxItems ? [checkMaxItems(x, schema.maxItems)] : []), (schema.uniqueItems === true ? [checkUniqueItems(x)] : [])); | ||
function generateChecks(jx, schema) { | ||
var checks = __spread((schema.pattern ? [checkPattern(jx, schema.pattern)] : []), (schema.regexp | ||
? [checkRegexp(jx, schema.regexp)] | ||
: []), (schema.format ? [checkFormat(jx, schema.format)] : []), (schema.minLength ? [checkMinLength(jx, schema.minLength)] : []), (schema.maxLength ? [checkMaxLength(jx, schema.maxLength)] : []), (schema.minimum ? [checkMinimum(jx, schema.minimum)] : []), (schema.maximum ? [checkMaximum(jx, schema.maximum)] : []), (schema.multipleOf ? [checkMultipleOf(jx, schema.multipleOf)] : []), (schema.type === 'integer' ? [checkInteger(jx)] : []), (schema.minItems ? [checkMinItems(jx, schema.minItems)] : []), (schema.maxItems ? [checkMaxItems(jx, schema.maxItems)] : []), (schema.uniqueItems === true ? [checkUniqueItems(jx)] : [])); | ||
if (checks.length < 1) { | ||
@@ -373,3 +375,4 @@ return 'true'; | ||
default: | ||
return [notImplemented('', JSON.stringify(schema.type), 'type', true)]; | ||
notImplemented(JSON.stringify(schema.type), 'type'); | ||
return [gen.unknownType]; | ||
} | ||
@@ -580,6 +583,3 @@ }); | ||
if (isSupported(key, isRoot) !== true) { | ||
var escalate = notImplemented('', key, 'field'); | ||
if (escalate !== null) { | ||
return escalate; | ||
} | ||
notImplemented(key, 'field'); | ||
} | ||
@@ -677,3 +677,3 @@ } | ||
}, | ||
dec: gen.typeDeclaration(name, gen.brandCombinator(fromSchema(scem, true), function (x) { return generateChecks(x, scem); }, name), true), | ||
dec: gen.typeDeclaration(name, gen.brandCombinator(fromSchema(scem, true), function (jx) { return generateChecks(jx, scem); }, name), true), | ||
}, | ||
@@ -684,2 +684,5 @@ ]; | ||
function fromRoot(root) { | ||
if (root.hasOwnProperty('$schema') === false) { | ||
warning("missing $schema declaration"); | ||
} | ||
// root schema info is printed in the beginning of the file | ||
@@ -702,3 +705,3 @@ var title = defaultExport; | ||
? error('schema root can not be a $ref object') | ||
: fromSchema(root, true), function (x) { return generateChecks(x, root); }, defaultExport), true), | ||
: fromSchema(root, true), function (jx) { return generateChecks(jx, root); }, defaultExport), true), | ||
}, | ||
@@ -817,2 +820,3 @@ ]; | ||
supportedAtRoot = [ | ||
'$schema', | ||
'minimum', | ||
@@ -825,2 +829,3 @@ 'maximum', | ||
'regexp', | ||
'format', | ||
'minItems', | ||
@@ -851,7 +856,7 @@ 'maxItems', | ||
// eslint-disable-next-line fp/no-throw | ||
throw new Error('Balining because of errors'); | ||
throw new Error('Bailing because of errors'); | ||
} | ||
if (returnCode === ErrorCode.WARNING && args.strict) { | ||
// eslint-disable-next-line fp/no-throw | ||
throw new Error('Balining because of warnings'); | ||
throw new Error('Bailing because of warnings'); | ||
} | ||
@@ -858,0 +863,0 @@ return [4 /*yield*/, '/*']; |
#!/usr/bin/env node | ||
"use strict"; | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
var __assign = (this && this.__assign) || function () { | ||
@@ -54,9 +55,9 @@ __assign = Object.assign || function(t) { | ||
exports.main = exports.processFile = exports.emit = exports.parser = void 0; | ||
var crypto = require("crypto"); | ||
var fs = require("fs"); | ||
var glob_1 = require("glob"); | ||
var path = require("path"); | ||
var crypto = require("crypto"); | ||
var yargs = require("yargs"); | ||
var glob_1 = require("glob"); | ||
var iotsfjs_1 = require("./iotsfjs"); | ||
exports.parser = function (args) { | ||
var parser = function (args) { | ||
var argv = yargs(args) | ||
@@ -87,3 +88,4 @@ .option('inputFile', { type: 'string', demandOption: true }) | ||
}; | ||
exports.emit = function (outputFile, lines) { | ||
exports.parser = parser; | ||
var emit = function (outputFile, lines) { | ||
var e_1, _a; | ||
@@ -117,3 +119,4 @@ function createParentDir(file) { | ||
}; | ||
exports.processFile = function (argv, _a) { | ||
exports.emit = emit; | ||
var processFile = function (argv, _a) { | ||
var _b; | ||
@@ -135,2 +138,3 @@ var stderr = _a.stderr, stdout = _a.stdout; | ||
}; | ||
exports.processFile = processFile; | ||
function main(_a) { | ||
@@ -137,0 +141,0 @@ var args = _a.args, stderr = _a.stderr, stdout = _a.stdout; |
{ | ||
"name": "io-ts-from-json-schema", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Iotsfjs is a static code generation utility used for converting json schema files into static TypeScript types and io-ts runtime validators.", | ||
@@ -19,3 +19,3 @@ "main": "lib/cli.js", | ||
"lint": "yarn eslint", | ||
"ci": "yarn lint && yarn typecheck && yarn test && yarn build", | ||
"ci": "yarn typecheck && yarn test && yarn lint && yarn build", | ||
"deploy-npm": "yarn ci && yarn publish", | ||
@@ -33,18 +33,20 @@ "deploy-alpha": "yarn deploy-npm --tag alpha" | ||
"@types/yargs": "^15.0.5", | ||
"@typescript-eslint/eslint-plugin": "^2.33.0", | ||
"@typescript-eslint/parser": "^2.33.0", | ||
"eslint": "6", | ||
"eslint-config-maasglobal-ts": "^0.0.6", | ||
"eslint-config-prettier": "^6.11.0", | ||
"@typescript-eslint/eslint-plugin": "^4.3.0", | ||
"@typescript-eslint/parser": "^4.3.0", | ||
"@typescript-eslint/typescript-estree": "^4.3.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-maasglobal-ts": "^0.0.8", | ||
"eslint-config-prettier": "^6.12.0", | ||
"eslint-plugin-fp": "^2.3.0", | ||
"eslint-plugin-import": "^2.20.2", | ||
"eslint-plugin-json": "^2.1.1", | ||
"eslint-plugin-prettier": "^3.1.3", | ||
"fp-ts": "^2.6.1", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-json": "^2.1.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"eslint-plugin-simple-import-sort": "^5.0.3", | ||
"fp-ts": "^2.9.0", | ||
"jest": "^26.4.2", | ||
"maas-schemas-git-develop": "git://github.com/cyberixae/maas-schemas.git#add_explicit_type_to_arrays_and_objects", | ||
"monocle-ts": "^2.1.0", | ||
"prettier": "^2.0.5", | ||
"prettier": "^2.1.2", | ||
"ts-jest": "^26.3.0", | ||
"typescript": "^3.9.2" | ||
"typescript": "^4.1.2" | ||
}, | ||
@@ -51,0 +53,0 @@ "repository": { |
62999
1388
21