<commit_before>require "active_support/core_ext/string"

describe JsonMatchers, "#match_json_schema" do
  it "fails with an invalid JSON schema" do
    schema = create(:schema, :invalid)

    json = build(:response)

    expect {
      expect(json).to match_json_schema(schema)
    }.to raise_error(JsonMatchers::InvalidSchemaError)
  end

  it "does not fail with an empty JSON body" do
    schema = create(:schema, {})

    json = build(:response, {})

    expect(json).to match_json_schema(schema)
  end

  it "supports asserting with the match_response_schema alias" do
    schema = create(:schema, :location)

    json = build(:response, :invalid_location)

    expect(json).not_to match_response_schema(schema)
  end

  it "supports refuting with the match_response_schema alias" do
    schema = create(:schema, :location)

    json = build(:response, :invalid_location)

    expect(json).not_to match_response_schema(schema)
  end

  it "fails when the body contains a property with the wrong type" do
    schema = create(:schema, :location)

    json = build(:response, :invalid_location)

    expect(json).not_to match_json_schema(schema)
  end

  it "fails when the body is missing a required property" do
    schema = create(:schema, :location)

    json = build(:response, {})

    expect(json).not_to match_json_schema(schema)
  end

  it "can reference a schema in a directory" do
    create(:schema, :location, name: "api/v1/schema")

    json = build(:response, :location)

    expect(json).to match_json_schema("api/v1/schema")
  end

  context "when passed a Hash" do<commit_msg>Add coverage for JSON Schema V4<commit_after>require "active_support/core_ext/string"

describe JsonMatchers, "#match_json_schema" do
  it "fails with an invalid JSON schema" do
    schema = create(:schema, :invalid)

    json = build(:response)

    expect {
      expect(json).to match_json_schema(schema)
    }.to raise_error(JsonMatchers::InvalidSchemaError)
  end

  it "does not fail with an empty JSON body" do
    schema = create(:schema, {})

    json = build(:response, {})

    expect(json).to match_json_schema(schema)
  end

  it "supports asserting with the match_response_schema alias" do
    schema = create(:schema, :location)

    json = build(:response, :invalid_location)

    expect(json).not_to match_response_schema(schema)
  end

  it "supports refuting with the match_response_schema alias" do
    schema = create(:schema, :location)

    json = build(:response, :invalid_location)

    expect(json).not_to match_response_schema(schema)
  end

  it "fails when the body contains a property with the wrong type" do
    schema = create(:schema, :location)

    json = build(:response, :invalid_location)

    expect(json).not_to match_json_schema(schema)
  end

  it "fails when the body is missing a required property" do
    schema = create(:schema, :location)

    json = build(:response, {})

    expect(json).not_to match_json_schema(schema)
  end

  it "can reference a schema in a directory" do
    create(:schema, :location, name: "api/v1/schema")

    json = build(:response, :location)

    expect(json).to match_json_schema("api/v1/schema")
  end

  it "supports invalidating the referenced schema when using local references" do
    create(:schema, name: "post", json: {
      "$schema": "https://json-schema.org/draft-04/schema#",
      "id": "file:/post.json#",
      "definitions": {
        "attributes": {
          "type": "object",
          "required": [
            "id",
            "name",
          ],
          "properties": {
            "id": { "type": "string" },
            "name": { "type": "string" }
          }
        }
      },
      "type": "object",
      "required": [
        "id",
        "type",
        "attributes"
      ],
      "properties": {
        "id": { "type": "string" },
        "type": { "type": "string" },
        "attributes": {
          "$ref": "#/definitions/attributes",
        }
      }
    })
    posts_index = create(:schema, name: "posts/index", json: {
      "$schema": "https://json-schema.org/draft-04/schema#",
      "id": "file:/posts/index.json#",
      "type": "object",
      "required": [
        "data"
      ],
      "definitions": {
        "posts": {
          "type": "array",
          "items": {
            "$ref": "file:/post.json#"
          }
        }
      },
      "properties": {
        "data": {
          "$ref": "#/definitions/posts"
        }
      }
    })

    json = build(:response, {
      "data": [{
        "id": "1",
        "type": "Post",
        "attributes": {
          "id": 1,
          "name": "The Post's Name"
        }
      }]
    })

    expect(json).not_to match_json_schema(posts_index)
  end

  it "can reference a schema relatively" do
    create(:schema, name: "post", json: {
      "$schema": "https://json-schema.org/draft-04/schema#",
      "id": "file:/post.json#",
      "type": "object",
      "required": [
        "id",
        "type",
        "attributes"
      ],
      "properties": {
        "id": { "type": "string" },
        "type": { "type": "string" },
        "attributes": {
          "type": "object",
          "required": [
            "id",
            "name"
          ],
          "properties": {
            "id": { "type": "string" },
            "name": { "type": "string" },
            "user": {
              "type": "object",
              "required": [
                "id"
              ],
              "properties": {
                "id": { "type": "string" }
              }
            }
          }
        }
      }
    })
    posts_index = create(:schema, name: "posts/index", json: {
      "$schema": "https://json-schema.org/draft-04/schema#",
      "id": "file:/posts/index.json#",
      "type": "object",
      "required": [
        "data"
      ],
      "definitions": {
        "posts": {
          "type": "array",
          "items": {
            "$ref": "file:/post.json#"
          }
        }
      },
      "properties": {
        "data": {
          "$ref": "#/definitions/posts"
        }
      }
    })

    json = build(:response, {
      "data": [{
        "id": "1",
        "type": "Post",
        "attributes": {
          "id": "1",
          "name": "<NAME>",
          "user": {
            "id": "1"
          }
        }
      }]
    })

    expect(json).to match_json_schema(posts_index)
  end

  context "when passed a Hash" do
{'fraction_non_alphanumeric': 0.15017859916136045, 'fraction_numerical': 0.00279546513433763, 'mean_word_length': 2.138401559454191, 'pattern_counts': {'":': 81, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 4, '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': '7381902', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1960, 'n_words': 470}