Giter Club home page Giter Club logo

ramldt2jsonschema's Introduction

ramldt2jsonschema

Greenkeeper badge NPM version NPM downloads Build status Test coverage

CLI & Library to convert a RAML 1.0 DataType to a JSON Schema, and back. Uses webapi-parser under the hood.

Usage

Global (CLI)

npm install -g ramldt2jsonschema

This will install two command-line tools:

  • dt2js: RAML data type <> JSON schema
  • js2dt: JSON schema <> RAML data type

dt2js

dt2js <ramlFile> <ramlTypeName> --draft=[version] [--validate]

Options

  • <ramlFile> Path to a file containing at least one RAML data type (e.g. path/to/api.raml)
  • <ramlTypeName> RAML type name to convert to JSON schema
  • --draft Optional JSON Shema draft version to convert to. Supported values are: 04, 06 and 07 (default)
  • --validate Validate output JSON Schema with Ajv. Throws an error if schema is invalid. Requires "ajv" to be installed. (default: false)

js2dt

js2dt <jsonFile> <ramlTypeName> [--validate]

Options

  • <jsonFile> Path to a JSON schema file (e.g. path/to/schema.json)
  • <ramlTypeName> RAML type name to give to the exported RAML data type
  • --validate Validate output RAML with webapi-parser. Throws an error if it is invalid. (default: false)

Locally (JavaScript)

npm install ramldt2jsonschema --save

dt2js

const r2j = require('ramldt2jsonschema')
const join = require('path').join
const fs = require('fs')

const filePath = join(__dirname, 'complex_cat.raml')
const ramlData = fs.readFileSync(filePath).toString()

async function main () {
  let schema
  try {
    schema = await r2j.dt2js(ramlData, 'Cat')
  } catch (err) {
    console.log(err)
    return
  }
  console.log(JSON.stringify(schema, null, 2))
}

main()

js2dt

const r2j = require('ramldt2jsonschema')
const join = require('path').join
const fs = require('fs')
const yaml = require('js-yaml')

const filePath = join(__dirname, 'complex_cat.json')
const jsonData = fs.readFileSync(filePath).toString()

async function main () {
  let raml
  try {
    raml = await r2j.js2dt(jsonData, 'Cat')
  } catch (err) {
    console.log(err)
    return
  }
  console.log('#%RAML 1.0 Library\n')
  console.log(yaml.safeDump(raml, { 'noRefs': true }))
}

main()

Resolving references

When the input contains external references (!include, uses:, $ref, etc.) and the referred files are not in the same directory as the script it is being ran from, you may provide a third argument to both dt2js and js2dt. The argument must be an object with a basePath key. All references will then be resolved relative to that base path.

Example of using basePath argument in dt2js:

// Script below ran from /home/john/where/ever/
// Reference is located at /home/john/schemas/simple_person.json
const raml2json = require('ramldt2jsonschema')

const ramlStr = `
  #%RAML 1.0 Library

  types:
    Person: !include simple_person.json
`
const basePath = '/home/john/schemas/' // or '../../schemas/'
const schema = raml2json.dt2js(ramlStr, 'Person', { basePath: basePath })
console.log(JSON.stringify(schema, null, 2))

Limitations

  • in js2dt
    • the following JSON Schema properties are not supported and as a result, may not be converted as expected:

      dependencies, exclusiveMaximum, exclusiveMinimum, items (array value), allOf, oneOf, not, format (email, hostname, ipv4, ipv6, uri), readOnly

    • the following JSON Schema properties won't be converted at all:

      $schema, additionalItems, contains, id, $id, propertyNames, definitions, links, fragmentResolution, media, pathStart, targetSchema

    • array items property is not properly converted to RAML when it's value is an array of schemas (see #111)

License

Apache 2.0

ramldt2jsonschema's People

Contributors

arslucidum avatar brevity avatar darkrodry avatar dependabot[bot] avatar forsakenharmony avatar greenkeeper[bot] avatar jstoiko avatar postatum avatar sichvoge avatar svc-scm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ramldt2jsonschema's Issues

dt2js: wrong JSON schema from RAML Array of union type

In RAML I have an array type with item type "Device" that is the union of 2 other types "Phone" and "Notebook". (types are from example in raml spec only array is new)
My array type seems to be converted to an JSON schema that does not describe the same data.

This is the RAML:

#%RAML 1.0
title: My API With Types
types:
  Phone:
    type: object
    properties:
      manufacturer:
        type: string
      numberOfSIMCards:
        type: number
      kind: string
  Notebook:
    type: object
    properties:
      manufacturer:
        type: string
      numberOfUSBPorts:
        type: number
      kind: string
  Device:
    type: Phone | Notebook
  Devices: Device[]

For type "Devices", dt2js produces this JSON Schema :

{
  "anyOf": [
    {
      "type": "array",
      "items": {
        "properties": {
          "manufacturer": {
            "type": "string"
          },
          "numberOfSIMCards": {
            "type": "number"
          },
          "kind": {
            "type": "string"
          }
        },
        "additionalProperties": true,
        "type": "object",
        "required": [
          "manufacturer",
          "numberOfSIMCards",
          "kind"
        ]
      }
    },
    {
      "type": "array",
      "items": {
        "properties": {
          "manufacturer": {
            "type": "string"
          },
          "numberOfUSBPorts": {
            "type": "number"
          },
          "kind": {
            "type": "string"
          }
        },
        "additionalProperties": true,
        "type": "object",
        "required": [
          "manufacturer",
          "numberOfUSBPorts",
          "kind"
        ]
      }
    }
  ],
  "type": "object",
  "$schema": "http://json-schema.org/draft-04/schema#"
}

I think the type should be array and not object, and "any-of" should be on items level.

Shouldn't the schema look like this instead?

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {
    "anyOf": [
      {
        "properties": {
          "manufacturer": {
            "type": "string"
          },
          "numberOfSIMCards": {
            "type": "number"
          },
          "kind": {
            "type": "string"
          }
        },
        "additionalProperties": true,
        "type": "object",
        "required": [
          "manufacturer",
          "numberOfSIMCards",
          "kind"
        ]
      },
      {
        "properties": {
          "manufacturer": {
            "type": "string"
          },
          "numberOfUSBPorts": {
            "type": "number"
          },
          "kind": {
            "type": "string"
          }
        },
        "additionalProperties": true,
        "type": "object",
        "required": [
          "manufacturer",
          "numberOfUSBPorts",
          "kind"
        ]
      }
    ]
  }
}

Silent error parsing included files

I've found a silent error that is produced when trying to parse a file that !includes other datatypes.

Let's say I have the following library:

#%RAML 1.0 Library

types:
  Example: 
    properties:
    foo:
      type: string
    bar: !include ./referencedFile.raml

And the referenced file:


#%RAML 1.0 DataType
type: array
items: string


The result, is, as expected:

{
  "foo": {
    "type": "string"
  },
  "bar": {
    "type": "array",
    "items": "string"
  },
  "type": "any",
  "$schema": "http://json-schema.org/draft-06/schema#"
}

So far, so good. Now, let's introduce a typo in the referenced file (a missing space between "type" and "array":


#%RAML 1.0 DataType
type:array
items: string


You would expect an error while parsing, however the result is this:

{
  "foo": {
    "type": "string"
  },
  "type": "any",
  "$schema": "http://json-schema.org/draft-06/schema#"
}

That is to say, the included file is simply ignored while parsing, with no warning to the fact that the result is missing the bar property.

The error happens because this project uses yaml-ast-parser to generate the AST that is later traversed. If it finds a path to a different file, it calls yaml-ast-parser again to said path and includes the result. However, during this second call there is no check to ensure that the yaml has been correctly parsed.

I'm creating a PR with a fix, so that an error is thrown to warn the user.

Union type is broken when union with array v0.1.4

See the example:

raml type file:

types:
  Statistic:
    type: object
    properties:
      id: integer
      count: integer | integer[]

I get as json schema:

{
  "type": "object",
  "anyOf": [
    {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "count": {
          "type": "array",
          "items": {
            "type": "integer"
          }
        }
      },
      "additionalProperties": true,
      "required": [
        "id",
        "count"
      ]
    },
    {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "count": {
          "type": "array",
          "items": {
            "type": "integer"
          }
        }
      },
      "additionalProperties": true,
      "required": [
        "id",
        "count"
      ]
    }
  ],
  "$schema": "http://json-schema.org/draft-04/schema#"
}

count should be either integer or integer[] but the any of generated gives me for both integer[]

Throws RangeError

eeEntityLib.v1.0.txt

E:\Projects\atom\Brooklyn\EE\schemas>dt2js eeEntityLib.v1.0.raml identityEntity
RangeError: Maximum call stack size exceeded
at RegExp.[Symbol.replace] (native)
at String.replace (native)
at Object.cljs$core$quote_string [as quote_string] (C:\Users\DavisChan\AppData\Roaming\npm\node_modules\ramldt2jsonschema\node_modules\datatype-expansion\node\cljs\core.js:30659:45)
at Object.cljs$core$pr_writer_impl [as pr_writer_impl] (C:\Users\DavisChan\AppData\Roaming\npm\node_modules\ramldt2jsonschema\node_modules\datatype-expansion\node\cljs\core.js:30717:42)
at cljs$core$pr_writer (C:\Users\DavisChan\AppData\Roaming\npm\node_modules\ramldt2jsonschema\node_modules\datatype-expansion\node\cljs\core.js:30792:18)
at C:\Users\DavisChan\AppData\Roaming\npm\node_modules\ramldt2jsonschema\node_modules\datatype-expansion\node\cljs\core.js:31216:142
at Object.cljs$core$pr_sequential_writer [as pr_sequential_writer] (C:\Users\DavisChan\AppData\Roaming\npm\node_modules\ramldt2jsonschema\node_modules\datatype-expansion\node\cljs\core.js:30522:142)
at Object.cljs$core$print_map [as print_map] (C:\Users\DavisChan\AppData\Roaming\npm\node_modules\ramldt2jsonschema\node_modules\datatype-expansion\node\cljs\core.js:31212:18)
at cljs.core.PersistentHashMap.cljs$core$IPrintWithWriter$pr_writer$arity$3 (C:\Users\DavisChan\AppData\Roaming\npm\node_modules\ramldt2jsonschema\node_modules\datatype-expansion\node\cljs\core.js:31334:18)
at Object.cljs$core$pr_str_STAR
[as pr_str_STAR_] (C:\Users\DavisChan\AppData\Roaming\npm\node_modules\ramldt2jsonschema\node_modules\datatype-expansion\node\cljs\core.js:3164:5)

including files with "use" does not seem to work

I get an error if I include files defining types using

uses:
ResourceLib: ResourceLib.raml

I get an error stating it does not know a type defined in the second file
Error: could not resolve: ResourceState

An in-range update of commander is breaking the build 🚨

The dependency commander was updated from 2.18.0 to 2.19.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

commander is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v2.19.0
  • Removed newline after Options and Commands headers (#864)
  • Bugfix - Error output (#862)
  • Fix to change default value to string (#856)
Commits

The new version differs by 6 commits.

  • 78b7dbd version bump 2.19.0
  • 6aafa20 prefixed error messages with "error:"
  • 6c0c1f6 removed newline above and below errors
  • b6549f2 removed indentation from errors
  • 2c20e91 removed newline after options and commands headers
  • 4c294c1 Fix to change default value to string

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

dt2js generates invalid schema in v0.1.1: maxLength is string instead of integer

In version v0.1.1, dt2js seems to generate invalid JSON schema.
sample code:

const raml2json = require('ramldt2jsonschema');
const Ajv = require('ajv');
const raml =`
#%RAML 1.0
title: test
types:
  BookId:
    type: string
    maxLength: 256`;
raml2json.dt2js(raml, 'BookId', function (err, schema) {
  if(err){
    console.error(err);
  }else{
    console.log(JSON.stringify(schema,null,2));
    const ajv = new Ajv({ allErrors:   true});
    ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
    ajv.compile(schema);
  }
});

Generated schema:

{
  "type": "string",
  "maxLength": "256",
  "$schema": "http://json-schema.org/draft-04/schema#"
}

In the schema, the value of maxLength is a string. This seems to be an error.
The JSON Schema specification states that the value of maxLength "MUST be a non-negative integer".
As a consequence, the attempt to compile the schema with ajv fails with message : Error: schema is invalid: data.maxLength should be integer

With the previous version of ramldt2jsonschema this error does not occur.

new version is currently broken on NPM (v0.2.0)

Hi again! Version 0.2.0 can't be downloaded through npm, with error:

error path (mypersonalfolder)/node_modules/ramldt2jsonschema/bin/dt2js.js
error code ENOENT
error errno -2
error syscall chmod
error enoent ENOENT: no such file or directory, chmod ' (mypersonalfolder)/node_modules/ramldt2jsonschema/bin/dt2js.js'

The error comes from commit 73a3eaede16b8fadd31d5891a55d3f3d8fa9c5c0, and specifically, from the change made to package.json: it has moved files to a bin folder, yet those files aren't included in the "files" section, so they aren't downloaded:

"files": [
"src/**"
],

The bin directory must be included there too, or else the whole download will fail when it tries to find those files to chmod them.

Tests (v1.0)

  • add initial unit tests/lint
  • test dt2js output in JSON schema validators
  • test js2dt output in RAML validator
  • add missing RAML features to complex RAML example
  • ~100% test coverage
  • setup travis automated tests
  • add integration test that will js->raml example files and parse them with raml-js-parser-2 { rejectOnErrors: true } to perform validation
  • add integration tests for raml->js (validate JSON schemas against main draft4 JSON schema?)

Enum is supported in dt2js but not js2dt

This works:

test/examples/type_conversion_test.raml:

types:
  TimeRange:
    type: string
    enum:
      - "1"
      - "30"

npx ramldt2jsonschema test/examples/type_conversion_test.raml TimeRange

{
  "type": "string",
  "enum": [
    "1",
    "30"
  ],
  "$schema": "http://json-schema.org/draft-06/schema#"
}

This does not:

test/examples/type_conversion_test.json:

{
  "type": "string",
  "enum": [
    "1",
    "30"
  ],
  "$schema": "http://json-schema.org/draft-06/schema#"
}

npx --package ramldt2jsonschema js2dt test/examples/type_conversion_test.json TimeRange

#%RAML 1.0 Library
types:
  TimeRange:
    type: string

js2dt: Invalid conversion of "additionalProperties"

There's a difference about the "additionalProperties" facet in jsonschema and the RAML specification.

In Jschonschema it's either a boolean or a JSON schema (or a reference), that additional properties must obey.

In the RAML specification, it's only a boolean. The type of additional properties has to be specified as:

#%RAML 1.0
title: My API With Types
types:
  Person:
    properties:
      name:
        required: true
        type: string
      age:
        required: false
        type: number
      //: # force all additional properties to be a string
        type: string

So, in this example:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "Test": {
      "description": "test",
      "type": "object",
      "properties": {
        "test": {
          "description": "test.test",
          "type": "object",
          "title": "test",
          "additionalProperties": {}
        }
      }
    }
  }
}

if the included additionalProperties-facet is...

  • of type boolean: just copy it to the RAML output
  • empty object: set additionalProperties to "true" in the RAML output
  • a reference: set additionalProperties to "false" in the RAML output and add a new property like this:
(...)
properties:
  //:
    type: <referenced type from json>
(..)

support for remote $ref in js2dt

E.g.

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "description": "JSON Schema with a remote $ref",
    "type": "object",
    "properties": {
        "geo": { "$ref": "http://json-schema.org/geo" }
    }
}

should fetch the JSON Schema referenced in $ref

Initial package setup

  • setup npm package
  • look at typescript and Java code for json writers
  • JS vs CLJS (update: went with JS)

Error when type does not exist is confusing

test.raml:

#%RAML 1.0
title: My API With Types

types:
  One: string

then running the command on a non-existing type:

$ dt2js test.raml Two
Error: form can only be a string or an object

The error seems to be coming directly from datatype-expansion. Since it does't provide any value, it should be caught and replace by something more explicit like: Type "Two" does not exist in "test.raml"

Some improvements

JSON->RAML DT:

  • convert JSON object.patternProperties to RAML pattern properties
  • drop JSON object.dependencies because RAML doesn't define anything similar
  • drop JSON exclusiveMaximum, exclusiveMinimum because RAML doesn't separate limits on inclusive-exclusive
  • drop JSON array.additionalItems because RAML doesn't support tuple validation of arrays
  • somehow convert JSON's 'format' to RAML. Maybe convert it to string with proper regexp in pattern.
  • Convert RAML displayName to JSON Schema title and back

Misc:

  • if fileTypes in dt2js script will be converted to JSON is some special way, it should be converted back to RAML in js2dt

EDIT: moved some of the improvements originally listed here to #18

An in-range update of datatype-expansion is breaking the build 🚨

The dependency datatype-expansion was updated from 0.3.4 to 0.3.5.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

datatype-expansion is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 15 commits.

  • 3ed5153 Removing external-helpers dependency
  • 026e66d update babel/rollup config
  • 65c67a4 downgrading rollup-plugin-babel to 3.x
  • b54fffc 0.3.5
  • 526e418 standard --fix
  • b6b8cde Merge pull request #104 from raml-org/greenkeeper/standard-12.0.0
  • 9db9826 Merge branch 'develop' into greenkeeper/standard-12.0.0
  • 66a2272 Merge pull request #103 from raml-org/greenkeeper/rollup-plugin-babel-4.0.0
  • b31f676 Merge branch 'master' into greenkeeper/rollup-plugin-babel-4.0.0
  • fabd989 Merge pull request #102 from raml-org/greenkeeper/rollup-0.60.0
  • 62852f6 Merge pull request #101 from raml-org/greenkeeper/rollup-plugin-uglify-4.0.0
  • 326fe21 chore(package): update standard to version 12.0.0
  • 33b8c29 chore(package): update rollup-plugin-babel to version 4.0.1
  • 4f4cc9c chore(package): update rollup to version 0.60.0
  • 421483e chore(package): update rollup-plugin-uglify to version 4.0.0

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Improvements to js2dt

  • find a way to handle JSON schema not
  • figure out a better way to do conversion, specifically when doing round-trip conversions:
    • type string with media wasn't necessarily a RAML type file before
    • type string with datetime-like regexp of pattern wasn't necessarily any kind of RAML date before. Maybe it is supposed to be just a string with date pattern?
    • using exact match of pattern when converting date[times]s isn't the best way to determine RAML 'date/time' type

Cannot handle !include

A simple Hello World RAML project, that is modified only to !include a file, will generate this exception:

[~/git/tmp/raml-test]$ dt2js ./api.raml TestType { Error at generateError (/usr/local/lib/node_modules/ramldt2jsonschema/node_modules/js-yaml/lib/js-yaml/loader.js:162:10) at throwError (/usr/local/lib/node_modules/ramldt2jsonschema/node_modules/js-yaml/lib/js-yaml/loader.js:168:9) at composeNode (/usr/local/lib/node_modules/ramldt2jsonschema/node_modules/js-yaml/lib/js-yaml/loader.js:1397:7) at readBlockMapping (/usr/local/lib/node_modules/ramldt2jsonschema/node_modules/js-yaml/lib/js-yaml/loader.js:1056:11) at composeNode (/usr/local/lib/node_modules/ramldt2jsonschema/node_modules/js-yaml/lib/js-yaml/loader.js:1326:12) at readBlockMapping (/usr/local/lib/node_modules/ramldt2jsonschema/node_modules/js-yaml/lib/js-yaml/loader.js:1056:11) at composeNode (/usr/local/lib/node_modules/ramldt2jsonschema/node_modules/js-yaml/lib/js-yaml/loader.js:1326:12) at readDocument (/usr/local/lib/node_modules/ramldt2jsonschema/node_modules/js-yaml/lib/js-yaml/loader.js:1488:3) at loadDocuments (/usr/local/lib/node_modules/ramldt2jsonschema/node_modules/js-yaml/lib/js-yaml/loader.js:1544:5) at load (/usr/local/lib/node_modules/ramldt2jsonschema/node_modules/js-yaml/lib/js-yaml/loader.js:1561:19) name: 'YAMLException', reason: 'unknown tag !<!include>', mark: Mark { name: null, buffer: '#%RAML 1.0\ntitle: Test Raml DT to JS schema\nversion: v1\nbaseUri: http://api.samplehost.com\ntypes:\n TestType: !include type1.yml\n/helloWorld:\n get:\n responses:\n 200:\n body:\n application/json:\n example: |\n {\n "message" : "Hello World"\n }\n\u0000', position: 128, line: 5, column: 30 }, message: 'unknown tag !<!include> at line 6, column 31:\n TestType: !include type1.yml\n ^' }

Some improvements (part 2)

JSON Schema->RAML DT:

  • make output less verbose
    • remove required: true (RAML defaults to true)
    • replace required: false by <property>? [1]
    • for properties that only have a {"type" : <type>} defined in JSON schema, write it in the form <propertyName>: <type> in RAML

RAML DT->JSON Schema:

  • convert RAML pattern properties (here search for This pattern property) to JSON object.patternProperties
  • support for external includes

--
[1] see RAML 1.0 Property Declaration

add integration tests to CI/mocha

Currently have to run:

$ node test/integration/js2dt_integration_test.js 

and

$ node test/integration/dt2js_integration_test.js
  • this should be a script defined in package.json using mocha
  • and automatically ran by Travis

Doesnt handle simple RAML 1 example from site

This is probably already known, but the simple example of RAML 1 data types is not supported for the most part. This is a MUCH needed tool so I hope someone gets on to fixing this ASAP to fully support RAML 1.0 DataTypes. The below pasted example from the RAML 1 example documentation... if you try to convert Group-new, or Group-update, it works. But Group fails.

As well, there needs to be more example RAML 1.0 types that work completely, and as issues are fixed/features added, update the RAML 1.0 type examples that will work. Things like inheritance, includes, custom types, enumerations, etc should all have good examples to show what works with this tool.

  Group-base:
    usage: The base type for group records
    properties:
      name:
        required: true

  Group-new:
    usage: Data for creating a new group
    type: Group-base
    properties:
      ITAuth:
        description: Authorization received from IT
        required: true

  Group-update:
    usage: Changeable properties of an existing group
    properties:
      name:

  Group:
    usage: A group in the system
    type: [ Group-base, Record-base ]
    properties:
      groupnum:
        type: StaticGroupNums | DynamicGroupNum
        required: true

An in-range update of raml-1-parser is breaking the build 🚨

The devDependency raml-1-parser was updated from 1.1.47 to 1.1.48.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

raml-1-parser is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for 1.1.48

NPM installation: npm install [email protected]

Commits

The new version differs by 7 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

type conversion is broken with v0.1.3

See this example:

types:
    SearchQuery:
        type: object
        additionalProperties: false
        properties:
          from:
            type: integer
            default: 0
            minimum: 0
            maximum: 10
          partial:
            type: string
            enum: ["true", "false"]
            default: "true"

I get :

{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "from": {
      "type": "integer",
      "default": "0",
      "minimum": "0",
      "maximum": 10
    },
    "partial": {
      "type": "string",
      "enum": [
        "true",
        "false"
      ],
      "default": true
    }
  },
  "required": [],
  "$schema": "http://json-schema.org/draft-04/schema#"
}
  • the minimum/default attribute get's string, but should be integer (as the maximum attribute is btw)
  • for the partial attribute, I defined it as enum {"false", "true"} (as string not boolean) and the default value get's cast as boolean.

Wrong required with object and examples

Hi there!

I've found an error trying to parse an object with object properties and an example.

For example, I have a RAML library like:

#%RAML 1.0 Library

types:
  randomObject:
    type: object
    example: !include someExample.json
    properties:
      foo: 
        type: object
        required: false
        properties:
          bar: string

And 'someExample.json' like:

{
  "foo": {
    "bar": "lorem ipsum"
  }
}

So I expect to have a json schema with an empty required array for randomObject, but I get the following:

{
  "type": "object",
  "example": {
    "foo": {
      "bar": "lorem ipsum"
    }
  },
  "properties": {
    "foo": {
      "type": "object",
      "properties": {
        "bar": {
          "type": "string"
        }
      },
      "additionalProperties": true,
      "required": [
        "bar"
      ]
    }
  },
  "additionalProperties": true,
  "required": [
    "foo"
  ],
  "$schema": "http://json-schema.org/draft-06/schema#"
}

If I remove the example in the RAML file, I get the expected json schema output:

{
  "type": "object",
  "example": {},
  "properties": {
    "foo": {
      "type": "object",
      "properties": {
        "bar": {
          "type": "string"
        }
      },
      "additionalProperties": true,
      "required": [
        "bar"
      ]
    }
  },
  "additionalProperties": true,
  "$schema": "http://json-schema.org/draft-06/schema#"
}

I'm working in a fix for this problem right now. As I saw, the dt2js.js file parse the example facet using the same steps as the properties facet, expecting the required facet, so we need to check if this facet exist in the data to parse because datatype-expansion library set it to true if it is not defined inside the RAML file.

An in-range update of ajv is breaking the build 🚨

The devDependency ajv was updated from 6.5.3 to 6.5.4.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

ajv is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 8 commits.

  • 8578816 6.5.4
  • 5c41a84 Merge pull request #863 from epoberezkin/fix-861-property-names
  • c1f929b fix: propertyNames with empty schema, closes #861
  • 70362b9 test: failing test for #861
  • 12e1655 Merge pull request #862 from billytrend/patch-2
  • f01e92a Fixes grammar
  • 851b73c Merge pull request #858 from epoberezkin/greenkeeper/bluebird-pin-3.5.1
  • 9aa65f7 fix: pin bluebird to 3.5.1

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

JsonSchema -> RAML incorrect

Docs state:

Limitations
in js2dt,

the following JSON schema properties are not supported and as a result, will not be converted:
format, title, not

However the following jsonschema generates the below raml including the not supported "title", "format" etal

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Basis period",
  "type": "object",
  "properties": {
    "start": {
      "title": "Start date",
      "description": "Date your basis period began",
      "type": "string",
      "format": "date"
    },
    "end": {
      "title": "End date",
      "description": "Date your basis period ended",
      "type": "string",
      "format": "date"
    }
  },
  "required": [
    "start",
    "end"
  ],
  "additionalProperties": false
}

#%RAML 1.0 Library

types:
  BasisPeriod:
    title: Basis period
    type: object
    properties:
      start:
        title: Start date
        description: Date your basis period began
        type: string
        format: date
        required: true
      end:
        title: End date
        description: Date your basis period ended
        type: string
        format: date
        required: true
    additionalProperties: false

dt2js drops all `properties` when declaring inline type `object[]`

#%RAML 1.0 Library
types:
  myArr:
    type: object[]
    items:
      properties:
        Foo: string

produces:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {}
  },
  "$schema": "http://json-schema.org/draft-06/schema#"
}

with an empty properties object. It is also missing additionalProperties and required JSON Schema properties.

However, note that the equivalent:

#%RAML 1.0 Library
types:
  myObj:
    properties:
      Foo: string
  myArr:
    type: myObj[]

produces the expected output:

{
  "type": "array",
  "items": {
    "properties": {
      "Foo": {
        "type": "string"
      }
    },
    "type": "object",
    "additionalProperties": true,
    "required": [
      "Foo"
    ]
  },
  "$schema": "http://json-schema.org/draft-06/schema#"
}

JSON schema > RAML DT

Plan (simple cases):

  • Create CLI that accepts json file name and RAML type name (optional)
  • Implement handling of simple cases of JSON schema
  • Test/lint of code that handles simple cases
  • Add simple examples of JSON schema
  • Test against simple JSON schema examples

Plan (complex cases):

  • Implement handling of complex cases of JSON schema (anyOf, allOf, oneOf, definitions, $ref)
  • Test/lint/docs of code that handles complex cases

Algorithm (kind of):
Input: JSON object representing valid json schema and describing a single RAML data type
Output: printed to console - RAML library containing converted data type

  • iterate over [propname, propval] pairs of object.
  • if propval is an array:
    • 1. apply algorithm to each element of array and store results in new array variable conv_array
    • 2. replace original array with propval of variable conv_array
  • if propval is an object:
    • 1. apply algorithm to propval and store result in variable conv_obj
    • 2. replace original object with conv_obj
  • if propkey is 'type', propvalue should be converted::
    • 1. if propvalue is 'object' and object holding propkey has param 'anyOf', call func to handle union (function logic described and implemented further)
    • 2. if propvalue is 'null', convert it to 'nil'
    • 3. if propvalue is 'string' and object holding propkey has param 'media', convert it to 'file'. Remove 'media' param.
    • 4. if propvalue is 'string' and object holding propkey has param 'pattern':
      • 1. if pattern matches date-only regexp, convert to 'date-only'
      • 2. if pattern matches time-only regexp, convert to 'time-only'
      • 3. if pattern matches datetime-only regexp, convert to 'datetime-only'
      • 4. if pattern matches rfc2616 regexp, convert to 'datetime' with 'format' set to 'rfc2616'
      • 5. if pattern matches rfc3339 regexp, convert to 'datetime' with 'format' set to 'rfc3339'
      • 6. if pattern value matches one of above cases remove 'pattern'
      • 7. otherwise do nothing with pattern
  • if propkey is 'object' and object holding propkey has non-empty 'required' param:
    • 1. add 'required: true' to each property with name from 'required' list
    • 2. remove 'required' property from object holding propkey 'object'
  • atomic values of propkeys/propvalues should be converted to RAML as is
  • pop root '$schema' property
  • add root 'types' property which will hold rest of raml
  • put all type names under 'types' and above their body declaration
  • handling definitions and $ref:
    • process definitions the same way main type is processed
    • add each definition as titled type in root, under types
    • change altering of root keywords for main type to put it in proper place
    • if met ref like { "$ref": "#/definitions/address" }, pop it and replace it with name of type from processed definitions (don't validate that type exists).
    • required property must have valid value after replacing $ref
    • testing
  • handle anyOf (RAML union):
    • Note that it’s possible to “factor” out the common parts of the subschemas
    • move each schema from anyOf to a separate type and add type: typename1 | typename2 | ... to an object containing anyOf
    • remove anyOf
  • handle allOf (RAML multiple inheritance?) - same as anyOf but use multiple inheritance
  • handle oneOf (is it also RAML union?) - for now is handled the same way as anyOf

JsonSchema generated doesn't validate against draft 4

I'm generating some schemas and got this sample output:

{
  "type": "object",
  "properties": {
    "foo": {
      "types": {
        "foo": {
          "type": "object",
          "properties": {
            "aprop": "string",
            "anotherprop": "string"
          },
          "required": []
        }
      },
      "type": "any"
    },
    "bar": {
      "type": "string"
    }
  },
  "additionalProperties": true,
  "required": [
    "foo",
    "bar"
  ],
  "$schema": "http://json-schema.org/draft-04/schema#"
}

that output doesn't validate against draft 4 - nor any later draft.

I'm using this online validator, and getting several errors. At least some of them seem to be related to the use of "any" as a type.

I'm not too familiar with the different versions of the spec, but this SO post seems to indicate that "any" as a type was already out of use when that post was written (May 2013).

Is there something I'm missing?

RAML DT > JSON schema

  • create a dt2js CLI that accepts params and outputs result of datatype-expansion
  • write output of dt2js to a schema.json file
  • make it possible to use this tool from JS code
  • when used from JS code, result should be returned from function, NOT written to file
  • build a very complex RAML data type that contains ALL features and use that to verify what needs to be changed
  • collect JSON schema validation errors
  • test JSON schema against a few other validators
  • write code to turn scripts output into valid JSON schema
  • add support for fileTypes

Duplicate entry `items` in required array v0.1.3

Here is an example :

types:
  Foo:
    type: object
    properties:
      items: integer[]
      total_count: integer

I get

 {
  "type": "object",
  "properties": {
    "items": {
      "type": "array",
      "items": {
        "type": "integer"
      }
    },
    "total_count": {
      "type": "integer"
    }
  },
  "additionalProperties": true,
  "required": [
    "items",
    "items",
    "total_count"
  ],
  "$schema": "http://json-schema.org/draft-04/schema#"
}

Seems that's a very special case where I have items as object property

Develop a module for RAML data types <> JSON schema

Introducing RAML data types in RAML 1.0 was, and still is, a huge success in the RAML community. As this is not a new way to model data, but planned to accompany existing schemas by leveraging an easy to write and human readable language. To achieve that, someone must be able to easily convert a RAML data type into a JSON schema that he can use for his / her existing tool chain, or the other way around from JSON schema to RAML data types.

Our strategy is to develop the conversation between these two in phases.

Phase 1

In the first phase we will use an in-memory canonical form that gets generated from a set of RAML data types as input, to convert that into a single JSON schema. We will use a previous developed module that takes a set of RAML data types and the data type name you want to have a JSON schema to produce a single canonical form of that type which we can easily convert almost 1:1 into JSON. Almost, since there are a couple of things that needs to be mapped differently. That allows us to quickly evaluate the conversion process and gather feedback from our community.

The JSON schema > RAML data types will be a bit different. We plan to preserve all information + definitions that are separated and convert that directly into a RAML data type.

Planned release: 26th September (see milestone v1.0)

Phase 2

Probably, after collecting some feedback, we will change RAM data types > JSON schema to preserve more information including individual types without creating a canonical form first. Thats a lot harder since we have inline unions and inheritance that are currently not 1:1 in JSON schema.

Planned release: TBD

setBasePath form example doesn't work.

the repo's readme includes the following code as an example:

var raml2json = require('ramldt2jsonschema')
var join = require('path').join
var fs = require('fs')

var filePath = join(__dirname, 'api.raml')
var includesPath = '/different/path/to/includes/'
var ramlData = fs.readFileSync(filePath).toString()

raml2json.setBasePath(includesPath)
raml2json.dt2js(ramlData, 'Cat', function (err, schema) {
  if (err) {
    console.log(err)
    return
  }
  console.log(JSON.stringify(schema, null, 2))
})

however, setBasePath fails like as follows:

TypeError: raml2json.setBasePath is not a function.

Since raml2json only has two properties (dt2js and js2dt, both functions).

I see that dt2js.js does export the setBasePath function. However, that function isn't exposed by the export in index.js. Unless there's a reason why the function isn't currently exposed, I suggest changing index.js to something like this:

'use strict'

var dt2js = require('./dt2js')
var js2dt = require('./js2dt')
module.exports.dt2js = {}
module.exports.dt2js.dt2js = dt2js.dt2js
module.exports.dt2js.setBasePath = dt2js.setBasePath
module.exports.js2dt = js2dt.js2dt

After that fix, the path is correctly parsed, although there are still problems arising when there are multiple references in diferent files. I haven't made this a separate issue because I don't know whether it is an actual bug or a consecuence of my quick fix for the previous one.

Say that file A is in /folder1. I set the base path to /folder1/ . Said file references ../folder2/fileB. So far, everything is correctly parsed.

But if now fileB references ./fileC in its directory, the parser will take that as /folder1/fileC, instead of /folder2/fileC which is where it should be looking.

An in-range update of ajv is breaking the build 🚨

The devDependency ajv was updated from 6.5.4 to 6.5.5.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

ajv is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 7 commits.

  • 494026e 6.5.5
  • 6478687 Merge pull request #860 from ossdev07/master
  • 2acd5f3 Merge pull request #865 from jsdevel/adding-logger-to-ts
  • d5edde4 Merge pull request #871 from nwoltman/patch-1
  • 8d769b6 Remove duplicated character in email regex
  • ae68416 Adding logger to typescript Options declaration.
  • 85b7f52 replacing phantomjs with headless chrome

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

add ability for js2dt to produce a RAML DataType

js2dt_cli does not produce a #%RAML 1.0 DataType (as the readme as well as code seems to indicate that it should: https://github.com/raml-org/ramldt2jsonschema/blob/master/src/js2dt_cli.js#L40
). Instead it produces a #%RAML 1.0 Library fragment. Since I can only specify a single data type <ramlTypeName> as a CLI option (js2dt <jsonFile> <ramlTypeName>), why output a #%RAML 1.0 Library for that single data type when a #%RAML 1.0 DataType seems to be the more appropriate output? Regardless, would it be an idea to add support for outputting a #%RAML 1.0 DataType from the CLI?

I have a directory of JSON schema files, each describing a data type that is part of the response of an endpoint. I was having issues with documenting the response format using RAML external types since RAML then does not let me use external types as part of other type definitions.

This tool looked like it could help me:

  1. Convert each JSON schema files into RAML data type fragment file
js2dt ./type_a.json type_a > ./converted-types/type_a.raml
js2dt ./type_b.json type_b > ./converted-types/type_b.raml
...
  1. Create a RAML library, like so:
#%RAML 1.0 Library
types:
  type_a: !include converted-types/type_a.raml
  type_b: !include converted-types/type_b.raml
  1. Reference these library types in my master api.raml file with a uses statement.

However, with the current #%RAML 1.0 Library output of the tool, I am having troubles generating valid RAML with includes / using etc, I think because of chaining using statements of libraries.

OAGIS JSON Schema DRAFT 4 is not converting to RAML 1.0

The attached JSON Schema DRAFT 4 is not converting to RAML 1.0. It does use additionalProperties keyword, which I thought would not be converted. This is the output of js2dt which was generated from NIST's Semantic Refinement Tool:

js2dt Purchase-Order-oagis-id-dd32f9c6c01048a19e15c423c9c741ae.json Purchase-Order-oagis-id-dd32f9c6c01048a19e15c423c9c741ae.raml
#%RAML 1.0 Library
types:
Token: string
NormalizedString: string
String: string
Decimal: number
Integer: number
Xbt_BooleanType: boolean
Purchase-Order-oagis-id-dd32f9c6c01048a19e15c423c9c741ae.raml:
type: object
properties:
purchaseOrder: array
additionalProperties: false

Stops there, NO raml file is created.

Purchase-Order-oagis-id-dd32f9c6c01048a19e15c423c9c741ae.json.txt

type conversation is broken for integer & boolean : v0.1.2

See this example :

types:
    Foo:
        type: object
        additionalProperties: false
        properties:
          key1:
            type: integer
            default: 1
          key2:
            type: string
            required: false
          key3:
            type: boolean
            default: true

I get

{
  "type": "object",
  "additionalProperties": "false",
  "properties": {
    "key1": {
      "type": "integer",
      "default": "1"
    },
    "key2": {
      "type": "string"
    },
    "key3": {
      "type": "boolean",
      "default": "true"
    }
  },
  "required": [
    "key1",
    "key2",
    "key3"
  ],
  "$schema": "http://json-schema.org/draft-04/schema#"
}

boolean is not converted properly so additionalProperties not working, required property not working
integer either so default values not working

Validate JSON Schema output of dt2js

ajv can validate JSON Schemas so we might as well use it to validate the output to make sure it is valid. Might want to make this optional though.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.