{"size": 1696, "ext": "js", "max_stars_repo_path": "forms-flow-forms/src/models/FieldMatchAccessPermissionSchema.js", "max_stars_repo_name": "andrepestana-aot/forms-flow-ai", "max_stars_repo_stars_event_min_datetime": "2020-04-16T20:26:18.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-30T12:39:50.000Z", "max_issues_repo_path": "forms-flow-forms/src/models/FieldMatchAccessPermissionSchema.js", "max_issues_repo_name": "andrepestana-aot/forms-flow-ai", "max_issues_repo_issues_event_min_datetime": "2020-06-05T21:30:27.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-30T04:53:25.000Z", "max_forks_repo_path": "forms-flow-forms/src/models/FieldMatchAccessPermissionSchema.js", "max_forks_repo_name": "andrepestana-aot/forms-flow-ai", "max_forks_repo_forks_event_min_datetime": "2020-04-15T18:54:51.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-29T04:58:49.000Z"}
{"max_stars_count": 132, "max_issues_count": 102, "max_forks_count": 91, "avg_line_length": 29.2413793103, "max_line_length": 84, "alphanum_fraction": 0.4876179245}
'use strict';

module.exports = function(formio) {
  const hook = require('../util/hook')(formio);
  const typeError = 'Value does not match a selected type';

  // Defines the permissions schema for field match access form's permissions.
  return new formio.mongoose.Schema(hook.alter('fieldMatchAccessPermissionSchema', {
    formFieldPath: {
      type: String,
      required: function() {
        return (typeof this.formFieldPath === 'string') ? false : true;
      }
    },
    value: {
      type: String,
      required: function() {
        return (typeof this.value === 'string') ? false : true;
      }
    },
    operator: {
      type: String,
      enum: ['$eq', '$lt', '$gt', '$lte', '$gte', '$in'],
      default: '$eq'
    },
    valueType: {
      type: String,
      enum: ['string', 'number', 'boolean', '[string]', '[number]'],
      required: function() {
        return (typeof this.valueType === 'string') ? false : true;
      },
      default: 'string',
      validate: [
        {
          validator: function(type) {
            switch (type) {
              case 'number':
                return isFinite(Number(this.value));
              case 'boolean':
                return (this.value === 'true' || this.value === 'false');
              case '[number]':
                return this.value.replace(/(^,)|(,$)/g, '')
                           .split(',')
                           .map(val => Number(val))
                           .every(val => isFinite(val));
            }
          },
          message: typeError
        }
      ]
    },
    roles: {
      type: [formio.mongoose.Schema.Types.ObjectId],
      ref: 'role',
    }
  }), {_id: false});
};
