<commit_before># TODO: clone the input schema

deap = require "deap"

URI = require "./uri"

escape = (string) ->
  string
    .replace(/~0/g, "~")
    .replace(/~1/g, "/")
    .replace(/%25/g, "%")

class Context

  constructor: ({@pointer, @scope}) ->

  child: (token) ->
    new Context
      pointer: "#{@pointer}/#{token}"
      scope: @scope

  #change_scope: (id) ->
    #@scope = URI.resolve(@scope, id)

module.exports = class Validator

  constructor: (schemas...) ->
    @references = {}
    @tests = {}
    @unresolved = {}

    for schema in schemas
      @add(schema)


  add: (schema) ->
    @_schema = deap.clone(schema)

    if @_schema.id
      # Make sure the schema id always ends with "#"
      @_schema.id = @_schema.id.replace /#?$/, "#"

    context = new Context
      pointer: @_schema.id || "#"
      scope: @_schema.id || "#"
    @compile_references @_schema, context

    # We try one more time to resolve $ref values, because
    # a schema may have been defined after we initially
    # tried to resolve the $ref.
    for ref, {scope, uri} of @unresolved
      if schema = @resolve_ref(uri, scope)
        delete @unresolved[ref]
        @references[ref] = schema
    if Object.keys(@unresolved).length > 0
      console.log "Unresolvable $ref values:", @unresolved

    @_validate = @compile(@_schema, context)




  validate: (data) ->
    @validate_schema("#", data)

  validate_schema: (uri, data) ->
    if @tests[uri]
      valid: @tests[uri](data)
    else<commit_msg>stop storing to @_schema<commit_after># TODO: clone the input schema

deap = require "deap"

URI = require "./uri"

escape = (string) ->
  string
    .replace(/~0/g, "~")
    .replace(/~1/g, "/")
    .replace(/%25/g, "%")

class Context

  constructor: ({@pointer, @scope}) ->

  child: (token) ->
    new Context
      pointer: "#{@pointer}/#{token}"
      scope: @scope

  #change_scope: (id) ->
    #@scope = URI.resolve(@scope, id)

module.exports = class Validator

  constructor: (schemas...) ->
    @references = {}
    @tests = {}
    @unresolved = {}

    for schema in schemas
      @add(schema)


  add: (schema) ->
    schema = deap.clone(schema)

    if schema.id
      # Make sure the schema id always ends with "#"
      schema.id = schema.id.replace /#?$/, "#"

    context = new Context
      pointer: schema.id || "#"
      scope: schema.id || "#"
    @compile_references schema, context

    # We try one more time to resolve $ref values, because
    # a schema may have been defined after we initially
    # tried to resolve the $ref.
    for ref, {scope, uri} of @unresolved
      if found_schema = @resolve_ref(uri, scope)
        delete @unresolved[ref]
        @references[ref] = found_schema
    if Object.keys(@unresolved).length > 0
      console.log "Unresolvable $ref values:", @unresolved

    @compile(schema, context)




  validate: (data) ->
    @validate_schema("#", data)

  validate_schema: (uri, data) ->
    if @tests[uri]
      valid: @tests[uri](data)
    else
{'fraction_non_alphanumeric': 0.1545877659574468, 'fraction_numerical': 0.003324468085106383, 'mean_word_length': 2.6165865384615383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '4778343', 'n_tokens_mistral': 1008, 'n_tokens_neox': 968, 'n_words': 273}