<commit_before>'use strict'

const Joi = require('joi')
const BaseJsonService = require('../base-json')

const validatorSchema = Joi.object()
  .keys({
    schemaValidationMessages: Joi.array().items(
      Joi.object({
        level: Joi.string().required(),
        message: Joi.string().required(),
      }).required()
    ),
  })
  .required()

module.exports = class SwaggerValidatorService extends BaseJsonService {
  static render({ message, clr }) {
    return { message, color: clr }
  }

  static get route() {
    return {
      base: 'swagger/valid/2.0',
      pattern: ':scheme(http|https)?/:url*',
    }
  }

  static get defaultBadgeData() {
    return { label: 'swagger' }
  }

  async handle({ scheme, url }) {
    const json = await this.fetch({ scheme, urlF: url })
    const valMessages = json.schemaValidationMessages

    if (!valMessages || valMessages.length === 0) {
      return this.constructor.render({ message: 'valid', clr: 'brightgreen' })
    } else {
      return this.constructor.render({ message: 'invalid', clr: 'red' })
    }
  }

  async fetch({ scheme, urlF }) {
    const url = 'http://online.swagger.io/validator/debug'
    return this._requestJson({
      url,
      schema: validatorSchema,
      options: {
        qs: {
          url: `${scheme}://${urlF}`,
        },
      },
    })
  }

  static get category() {
    return 'other'
  }

  static get examples() {
    return [
      {
        title: 'Swagger Validator',
        staticExample: this.render({ message: 'valid', clr: 'brightgreen' }),
        namedParams: {
          scheme: 'https',
          url:
            'raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/json/petstore-expanded.json',
        },
      },
    ]
  }
}<commit_msg>fix swagger example (#2476)<commit_after>'use strict'

const Joi = require('joi')
const BaseJsonService = require('../base-json')

const validatorSchema = Joi.object()
  .keys({
    schemaValidationMessages: Joi.array().items(
      Joi.object({
        level: Joi.string().required(),
        message: Joi.string().required(),
      }).required()
    ),
  })
  .required()

module.exports = class SwaggerValidatorService extends BaseJsonService {
  static render({ message, clr }) {
    return { message, color: clr }
  }

  static get route() {
    return {
      base: 'swagger/valid/2.0',
      pattern: ':scheme(http|https)?/:url*',
    }
  }

  static get defaultBadgeData() {
    return { label: 'swagger' }
  }

  async handle({ scheme, url }) {
    const json = await this.fetch({ scheme, urlF: url })
    const valMessages = json.schemaValidationMessages

    if (!valMessages || valMessages.length === 0) {
      return this.constructor.render({ message: 'valid', clr: 'brightgreen' })
    } else {
      return this.constructor.render({ message: 'invalid', clr: 'red' })
    }
  }

  async fetch({ scheme, urlF }) {
    const url = 'http://online.swagger.io/validator/debug'
    return this._requestJson({
      url,
      schema: validatorSchema,
      options: {
        qs: {
          url: `${scheme}://${urlF}`,
        },
      },
    })
  }

  static get category() {
    return 'other'
  }

  static get examples() {
    return [
      {
        title: 'Swagger Validator',
        pattern: ':scheme/:url',
        staticExample: this.render({ message: 'valid', clr: 'brightgreen' }),
        namedParams: {
          scheme: 'https',
          url:
            'raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/json/petstore-expanded.json',
        },
      },
    ]
  }
}
{'fraction_non_alphanumeric': 0.15444661276833008, 'fraction_numerical': 0.0039029829941455256, 'mean_word_length': 2.4533205004812317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4894802', 'n_tokens_mistral': 1137, 'n_tokens_neox': 1113, 'n_words': 241}