Giter Club home page Giter Club logo

Comments (6)

postatum avatar postatum commented on June 13, 2024

@jstoiko is this issue still relevant after rework using webapi-parser?
We already validate dt2js output while running integration tests. IMO if integration tests pass, we can assume dt2js produces valid json.
I think this step should be performed during development/testing (as it is now) and not in users' projects.

from ramldt2jsonschema.

jstoiko avatar jstoiko commented on June 13, 2024

would it “cost” a lot to add a —validate option to dt2js and use AJV to validate? if not, I think it’s worth working on it for a future minor release.

from ramldt2jsonschema.

postatum avatar postatum commented on June 13, 2024

would it “cost” a lot to add a —validate option to dt2js and use AJV to validate?

It wouldn't.

if not, I think it’s worth working on it for a future minor release.

Ok

from ramldt2jsonschema.

postatum avatar postatum commented on June 13, 2024

@jstoiko should validation only work when using dt2js as CLI?
I think it should because when using it from js code:

  1. Users can validate output json schema by themselves if they want;
  2. Output of dt2js would become complicated because then it would have to be either validation report or json schema, and have some flag to show if schema is valid or not.

Or should it work both in CLI and js and throw an error when output schema is invalid, instead of returning a validation report?

from ramldt2jsonschema.

jstoiko avatar jstoiko commented on June 13, 2024

I have an idea. Would you be able to provide two simple examples, one showing dt2js CLI + output pipes using ajv-cli (or any other "standard"/popular JSON Schema validator), and the other one showing how to achieve the same in code. Let's see what it looks like. Maybe we don't need this feature at all as longs as they're easy ways to get to the same result.

from ramldt2jsonschema.

postatum avatar postatum commented on June 13, 2024

Below are code examples.
Validating output schema in JS code is pretty simple. Validating using CLI, on the other hand, is complicated. I didn't find any CLI libs to validate json schema other than ajv-cli. It doesn't have support for piping so a complex command has to be constructed to make it work with dt2js.

const r2j = require('ramldt2jsonschema')

const ramlData = `#%RAML 1.0 Library
types:
  Cat:
    type: object
    properties:
      name: string
      color: string`

async function main () {
  const schema = await r2j.dt2js(ramlData, 'Cat')
  schema.required = 123
  const Ajv = require('ajv')
  const ajv = new Ajv({ allErrors: true, schemaId: 'auto' })
  const valid = ajv.validateSchema(schema)
  if (!valid) {
    const errorsText = ajv.errorsText(ajv.errors, { separator: '; ' })
    throw new Error(`Invalid json: ${errorsText}`)
  }
}

main()
$ ajv -d <(dt2js ../test/integration/raml/complex_homeanimal.raml Cat) -s <(curl http://json-schema.org/draft-07/schema#)

from ramldt2jsonschema.

Related Issues (20)

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.