npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ramldt2jsonschema

v1.2.3

Published

Converts a RAML data type into JSON schema, and back

Downloads

228,834

Readme

ramldt2jsonschema

Greenkeeper badge NPM version NPM downloads Build status Test coverage

CLI & Library to convert a RAML 1.0 DataType to a JSON Schema Draft 4, 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