<commit_before>path = require("path")
fs = require("fs")

js2coffee = require("js2coffee")
JSV = require("JSV").JSV
jsv = JSV.createEnvironment("json-schema-draft-03")

schema = require("./schema")

example =
  paths:
    things:
      path: "/things"
      publish: true

  resources:
    things:
      actions:
        list:
          method: "GET"
          query:
            sort:
              type: "string"
              enum: ["asc", "desc"]
          response_schema: "foo_list"

  schema:
    id: "patchboard.example"
    thing:
      type: "object"
      properties:
        name:
          type: "string"
          required: true
    thing_list:
      type: "array"
      items: {$ref: "#thing"}

    
module.exports =

  validate: (api_file) ->
    api_file = path.resolve(api_file)
    try
      api = require(api_file)
      report = jsv.validate api, schema
      if report.errors.length == 0
        console.log "Valid API definition"
      else
        # TODO: provide more useful output, perhaps by finding and printing
        # the exact objects in the data and schema that are failing.
        # JSV may have some useful functions for this.
        console.log "Invalid API.  Errors:", report.errors
    catch error
      console.log "Problem reading API description:", error.message

  generate: (type) ->
    if type == "json"
      api = example
      string = JSON.stringify(api, null, 2)
      fs.writeFileSync("api.json", string)
    else if type == "coffee"
      api = example
      src = "modules.export = #{JSON.stringify(api)}"
      string = js2coffee.build(src)
      fs.writeFileSync("api.coffee", string)
    else
      console.log "Unsupported type: #{type}"
<commit_msg>bin/patchboard exits with 1 when validation fails<commit_after>path = require("path")
fs = require("fs")

js2coffee = require("js2coffee")
JSV = require("JSV").JSV
jsv = JSV.createEnvironment("json-schema-draft-03")

schema = require("./schema")

example =
  paths:
    things:
      path: "/things"
      publish: true

  resources:
    things:
      actions:
        list:
          method: "GET"
          query:
            sort:
              type: "string"
              enum: ["asc", "desc"]
          response_schema: "foo_list"

  schema:
    id: "patchboard.example"
    thing:
      type: "object"
      properties:
        name:
          type: "string"
          required: true
    thing_list:
      type: "array"
      items: {$ref: "#thing"}

    
module.exports =

  validate: (api_file) ->
    api_file = path.resolve(api_file)
    try
      api = require(api_file)
      report = jsv.validate api, schema
      if report.errors.length == 0
        console.log "Valid API definition"
      else
        # TODO: provide more useful output, perhaps by finding and printing
        # the exact objects in the data and schema that are failing.
        # JSV may have some useful functions for this.
        console.log "Invalid API.  Errors:", report.errors
        process.exit(1)
    catch error
      console.log "Problem reading API description:", error.message
      process.exit(1)

  generate: (type) ->
    if type == "json"
      api = example
      string = JSON.stringify(api, null, 2)
      fs.writeFileSync("api.json", string)
    else if type == "coffee"
      api = example
      src = "modules.export = #{JSON.stringify(api)}"
      string = js2coffee.build(src)
      fs.writeFileSync("api.coffee", string)
    else
      console.log "Unsupported type: #{type}"

{'fraction_non_alphanumeric': 0.11564820102798401, 'fraction_numerical': 0.0048543689320388345, 'mean_word_length': 2.1845454545454546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1413872', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1035, 'n_words': 298}