<commit_before>package schema

import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
	"testing"

	"github.com/xeipuuv/gojsonschema"
)

// SchemaValidator uses the gojsonschema library to validate the JSON encoding
// of Go objects against a pre-defined JSON schema.
type SchemaValidator struct {
	// Schema is the JSON schema to validate against.
	//
	// Subject is the instance of Go type that will be validated.
	Schema, Subject gojsonschema.JSONLoader
}

func NewSchemaValidator(t *testing.T, schemaName string, got interface{}) *SchemaValidator {
	dir, err := os.Getwd()
	if err != nil {
		t.Fatal(err)
	}
	// Platform compatibility: use "/" separators always for file://
	dir = strings.Replace(dir, "\\", "/", -1)

	schema := gojsonschema.NewReferenceLoader(fmt.Sprintf(
		"file:///%s/schema/%s", dir, schemaName),
	)

	marshalled, err := json.Marshal(got)
	if err != nil {
		t.Fatal(err)
	}

	subject := gojsonschema.NewStringLoader(string(marshalled))

	return &SchemaValidator{
		Schema:  schema,
		Subject: subject,
	}
}

// Validate validates a Go object against JSON schema in a testing environment.
// If the validation fails, then the test will fail after logging all of the
// validation errors experienced by the validator.<commit_msg>Only replace \ with / on Windows<commit_after>package schema

import (
	"encoding/json"
	"fmt"
	"os"
	"runtime"
	"strings"
	"testing"

	"github.com/xeipuuv/gojsonschema"
)

// SchemaValidator uses the gojsonschema library to validate the JSON encoding
// of Go objects against a pre-defined JSON schema.
type SchemaValidator struct {
	// Schema is the JSON schema to validate against.
	//
	// Subject is the instance of Go type that will be validated.
	Schema, Subject gojsonschema.JSONLoader
}

func NewSchemaValidator(t *testing.T, schemaName string, got interface{}) *SchemaValidator {
	dir, err := os.Getwd()
	if err != nil {
		t.Fatal(err)
	}

	if runtime.GOOS == "windows" {
		// Platform compatibility: use "/" separators always for file://
		dir = strings.Replace(dir, "\\", "/", -1)
	}

	schema := gojsonschema.NewReferenceLoader(fmt.Sprintf(
		"file:///%s/schema/%s", dir, schemaName),
	)

	marshalled, err := json.Marshal(got)
	if err != nil {
		t.Fatal(err)
	}

	subject := gojsonschema.NewStringLoader(string(marshalled))

	return &SchemaValidator{
		Schema:  schema,
		Subject: subject,
	}
}

// Validate validates a Go object against JSON schema in a testing environment.
// If the validation fails, then the test will fail after logging all of the
// validation errors experienced by the validator.
{'fraction_non_alphanumeric': 0.116796875, 'fraction_numerical': 0.00078125, 'mean_word_length': 3.859582542694497, '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': '2464450', 'n_tokens_mistral': 821, 'n_tokens_neox': 801, 'n_words': 265}