'use strict';
var formats = require('ajv/lib/compile/formats')();
var equal = require('ajv/lib/compile/equal');
var validate = (function() {
  var pattern0 = new RegExp('^resources/.*$');
  var refVal = [];
  var refVal1 = (function() {
    var pattern0 = new RegExp('^resources/.*$');
    return function validate(data, dataPath, parentData, parentDataProperty, rootData) {
      'use strict';
      var vErrors = null;
      var errors = 0;
      if (rootData === undefined) rootData = data;
      if ((data && typeof data === "object" && !Array.isArray(data))) {
        if (true) {
          var errs__0 = errors;
          var valid1 = true;
          if (data.type === undefined) {
            valid1 = false;
            validate.errors = [{
              keyword: 'required',
              dataPath: (dataPath || '') + "",
              schemaPath: '#/required',
              params: {
                missingProperty: 'type'
              },
              message: 'should have required property \'type\''
            }];
            return false;
          } else {
            var errs_1 = errors;
            var schema1 = validate.schema.properties.type.enum;
            var valid1;
            valid1 = false;
            for (var i1 = 0; i1 < schema1.length; i1++)
              if (equal(data.type, schema1[i1])) {
                valid1 = true;
                break;
              } if (!valid1) {
              validate.errors = [{
                keyword: 'enum',
                dataPath: (dataPath || '') + '.type',
                schemaPath: '#/properties/type/enum',
                params: {
                  allowedValues: schema1
                },
                message: 'should be equal to one of the allowed values'
              }];
              return false;
            } else {}
            if (errors === errs_1) {}
            var valid1 = errors === errs_1;
          }
          if (valid1) {
            if (data.body === undefined) {
              valid1 = false;
              validate.errors = [{
                keyword: 'required',
                dataPath: (dataPath || '') + "",
                schemaPath: '#/required',
                params: {
                  missingProperty: 'body'
                },
                message: 'should have required property \'body\''
              }];
              return false;
            } else {
              var errs_1 = errors;
              var valid1 = errors === errs_1;
            }
            if (valid1) {
              var data1 = data.path;
              if (data1 === undefined) {
                valid1 = false;
                validate.errors = [{
                  keyword: 'required',
                  dataPath: (dataPath || '') + "",
                  schemaPath: '#/required',
                  params: {
                    missingProperty: 'path'
                  },
                  message: 'should have required property \'path\''
                }];
                return false;
              } else {
                var errs_1 = errors;
                var errs_2 = errors;
                if ((typeof data1 === "number")) {
                  if (true) {}
                }
                if (errors === errs_2) {
                  if (typeof data1 === "string") {
                    if (!formats['uri-reference'].test(data1)) {
                      validate.errors = [{
                        keyword: 'format',
                        dataPath: (dataPath || '') + '.path',
                        schemaPath: '../../oada.schema.json#/definitions/path/format',
                        params: {
                          format: 'uri-reference'
                        },
                        message: 'should match format "uri-reference"'
                      }];
                      return false;
                    } else {}
                  } else {
                    validate.errors = [{
                      keyword: 'type',
                      dataPath: (dataPath || '') + '.path',
                      schemaPath: '../../oada.schema.json#/definitions/path/type',
                      params: {
                        type: 'string'
                      },
                      message: 'should be string'
                    }];
                    return false;
                  }
                  if (errors === errs_2) {}
                }
                var valid2 = errors === errs_2;
                if (valid2) {}
                if (errors === errs_1) {}
                var valid1 = errors === errs_1;
              }
              if (valid1) {
                var data1 = data.resource_id;
                if (data1 === undefined) {
                  valid1 = false;
                  validate.errors = [{
                    keyword: 'required',
                    dataPath: (dataPath || '') + "",
                    schemaPath: '#/required',
                    params: {
                      missingProperty: 'resource_id'
                    },
                    message: 'should have required property \'resource_id\''
                  }];
                  return false;
                } else {
                  var errs_1 = errors;
                  var errs_2 = errors;
                  if (typeof data1 === "string") {
                    if (!pattern0.test(data1)) {
                      validate.errors = [{
                        keyword: 'pattern',
                        dataPath: (dataPath || '') + '.resource_id',
                        schemaPath: '../../oada.schema.json#/definitions/_id/pattern',
                        params: {
                          pattern: '^resources/.*$'
                        },
                        message: 'should match pattern "^resources/.*$"'
                      }];
                      return false;
                    } else {}
                  } else {
                    validate.errors = [{
                      keyword: 'type',
                      dataPath: (dataPath || '') + '.resource_id',
                      schemaPath: '../../oada.schema.json#/definitions/_id/type',
                      params: {
                        type: 'string'
                      },
                      message: 'should be string'
                    }];
                    return false;
                  }
                  if (errors === errs_2) {}
                  var valid2 = errors === errs_2;
                  if (valid2) {}
                  if (errors === errs_1) {}
                  var valid1 = errors === errs_1;
                }
                if (valid1) {}
              }
            }
          }
          if (errs__0 == errors) {}
        }
      } else {
        validate.errors = [{
          keyword: 'type',
          dataPath: (dataPath || '') + "",
          schemaPath: '#/type',
          params: {
            type: 'object'
          },
          message: 'should be object'
        }];
        return false;
      }
      if (errors === 0) {}
      validate.errors = vErrors;
      return errors === 0;
    };
  })();
  refVal1.schema = {
    "type": "object",
    "required": ["type", "body", "path", "resource_id"],
    "properties": {
      "type": {
        "enum": ["merge", "delete"]
      },
      "body": {
        "$comment": "Should probably narrow this schema down",
        "description": "The contents of what was changed."
      },
      "path": {
        "$ref": "../../oada.schema.json#/definitions/path"
      },
      "resource_id": {
        "$ref": "../../oada.schema.json#/definitions/_id"
      }
    }
  };
  refVal1.errors = null;
  refVal[1] = refVal1;
  var refVal2 = {
    "description": "The represenation of a path within an OADA server",
    "type": "string",
    "format": "uri-reference"
  };
  refVal[2] = refVal2;
  var refVal3 = {
    "description": "_id identifies a resource in the OADA API.",
    "type": "string",
    "pattern": "^resources/.*$"
  };
  refVal[3] = refVal3;
  return function validate(data, dataPath, parentData, parentDataProperty, rootData) {
    'use strict'; /*# sourceURL=https://formats.openag.io/oada/change/v2.schema.json */
    var vErrors = null;
    var errors = 0;
    if (rootData === undefined) rootData = data;
    if (Array.isArray(data)) {
      var errs__0 = errors;
      var valid0;
      for (var i0 = 0; i0 < data.length; i0++) {
        var errs_1 = errors;
        if (!refVal1(data[i0], (dataPath || '') + '[' + i0 + ']', data, i0, rootData)) {
          if (vErrors === null) vErrors = refVal1.errors;
          else vErrors = vErrors.concat(refVal1.errors);
          errors = vErrors.length;
        } else {}
        if (errors === errs_1) {}
        var valid1 = errors === errs_1;
        if (!valid1) break;
      }
      if (errs__0 == errors) {}
    } else {
      validate.errors = [{
        keyword: 'type',
        dataPath: (dataPath || '') + "",
        schemaPath: '#/type',
        params: {
          type: 'array'
        },
        message: 'should be array'
      }];
      return false;
    }
    if (errors === 0) {}
    validate.errors = vErrors;
    return errors === 0;
  };
})();
validate.schema = {
  "$id": "https://formats.openag.io/oada/change/v2.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "change": {
      "type": "object",
      "required": ["type", "body", "path", "resource_id"],
      "properties": {
        "type": {
          "enum": ["merge", "delete"]
        },
        "body": {
          "$comment": "Should probably narrow this schema down",
          "description": "The contents of what was changed."
        },
        "path": {
          "$ref": "../../oada.schema.json#/definitions/path"
        },
        "resource_id": {
          "$ref": "../../oada.schema.json#/definitions/_id"
        }
      }
    }
  },
  "type": "array",
  "description": "Array-based change representation (OADA v2)",
  "items": {
    "$ref": "#/definitions/change"
  },
  "examples": [
    [{
      "resource_id": "resources/default:resources_bookmarks_123",
      "path": "",
      "body": {
        "rocks": {
          "_rev": 1
        },
        "_rev": 1
      },
      "type": "merge"
    }, {
      "resource_id": "resources/default:resources_rocks_123",
      "path": "/rocks",
      "body": {
        "test": "abc",
        "_rev": 1
      },
      "type": "merge"
    }]
  ]
};
validate.errors = null;
module.exports = validate;