Sha256: d2c8ec2f9e40f48cc8606bcb0a8c9e0cfb46cd575429b598b187f21366fe3f8f

Contents?: true

Size: 1.2 KB

Versions: 53

Compression:

Stored size: 1.2 KB

Contents

'use strict';

var CONSTRUCTORS = {
  Object: Object,
  Array: Array,
  Function: Function,
  Number: Number,
  String: String,
  Date: Date,
  RegExp: RegExp
};

module.exports = function defFunc(ajv) {
  /* istanbul ignore else */
  if (typeof Buffer != 'undefined')
    CONSTRUCTORS.Buffer = Buffer;

  defFunc.definition = {
    compile: function (schema) {
      if (typeof schema == 'string') {
        var Constructor = getConstructor(schema);
        return function (data) {
          return data instanceof Constructor;
        };
      }

      var constructors = schema.map(getConstructor);
      return function (data) {
        for (var i=0; i<constructors.length; i++)
          if (data instanceof constructors[i]) return true;
        return false;
      };
    },
    CONSTRUCTORS: CONSTRUCTORS,
    metaSchema: {
      anyOf: [
        { type: 'string' },
        {
          type: 'array',
          items: { type: 'string' }
        }
      ]
    }
  };

  ajv.addKeyword('instanceof', defFunc.definition);
  return ajv;

  function getConstructor(c) {
    var Constructor = CONSTRUCTORS[c];
    if (Constructor) return Constructor;
    throw new Error('invalid "instanceof" keyword value ' + c);
  }
};

Version data entries

53 entries across 51 versions & 5 rubygems

Version Path
ilog-0.4.1 node_modules/ajv-keywords/keywords/instanceof.js
ilog-0.4.0 node_modules/ajv-keywords/keywords/instanceof.js
ilog-0.3.3 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-18.0.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.21.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.20.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.19.1 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.19.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.18.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.17.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.16.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.15.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.14.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.13.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.12.2 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.12.1 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.12.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.11.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.10.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.9.0 node_modules/ajv-keywords/keywords/instanceof.js