Sha256: ddd9844876a8c010be3590dc9a8d2168e30b01c91f73fa4545dd517c6c97a01b

Contents?: true

Size: 1.8 KB

Versions: 88

Compression:

Stored size: 1.8 KB

Contents

'use strict';

var SCALAR_TYPES = ['number', 'integer', 'string', 'boolean', 'null'];

module.exports = function defFunc(ajv) {
  defFunc.definition = {
    type: 'array',
    compile: function(keys, parentSchema, it) {
      var equal = it.util.equal;
      var scalar = getScalarKeys(keys, parentSchema);

      return function(data) {
        if (data.length > 1) {
          for (var k=0; k < keys.length; k++) {
            var i, key = keys[k];
            if (scalar[k]) {
              var hash = {};
              for (i = data.length; i--;) {
                if (!data[i] || typeof data[i] != 'object') continue;
                var prop = data[i][key];
                if (prop && typeof prop == 'object') continue;
                if (typeof prop == 'string') prop = '"' + prop;
                if (hash[prop]) return false;
                hash[prop] = true;
              }
            } else {
              for (i = data.length; i--;) {
                if (!data[i] || typeof data[i] != 'object') continue;
                for (var j = i; j--;) {
                  if (data[j] && typeof data[j] == 'object' && equal(data[i][key], data[j][key]))
                    return false;
                }
              }
            }
          }
        }
        return true;
      };
    },
    metaSchema: {
      type: 'array',
      items: {type: 'string'}
    }
  };

  ajv.addKeyword('uniqueItemProperties', defFunc.definition);
  return ajv;
};


function getScalarKeys(keys, schema) {
  return keys.map(function(key) {
    var properties = schema.items && schema.items.properties;
    var propType = properties && properties[key] && properties[key].type;
    return Array.isArray(propType)
            ? propType.indexOf('object') < 0 && propType.indexOf('array') < 0
            : SCALAR_TYPES.indexOf(propType) >= 0;
  });
}

Version data entries

88 entries across 38 versions & 9 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.18.0 test/dummy/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.18.2 test/dummy/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.16.1 test/dummy/node_modules/@rails/webpacker/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.16.1 test/dummy/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.16.1 test/dummy/node_modules/compression-webpack-plugin/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.16.1 test/dummy/node_modules/file-loader/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.16.1 test/dummy/node_modules/sass-loader/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.16.1 test/dummy/node_modules/babel-loader/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.16.1 test/dummy/node_modules/css-loader/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.16.1 test/dummy/node_modules/style-loader/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.15.2 test/dummy/node_modules/@rails/webpacker/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.15.2 test/dummy/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.15.2 test/dummy/node_modules/file-loader/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.15.2 test/dummy/node_modules/css-loader/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.15.2 test/dummy/node_modules/sass-loader/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.15.2 test/dummy/node_modules/babel-loader/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.15.2 test/dummy/node_modules/compression-webpack-plugin/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.15.2 test/dummy/node_modules/style-loader/node_modules/ajv-keywords/keywords/uniqueItemProperties.js
disco_app-0.18.4 test/dummy/node_modules/@rails/webpacker/node_modules/ajv-keywords/keywords/uniqueItemProperties.js