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
govuk_publishing_components-17.8.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.7.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.6.1 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.6.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.5.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.4.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.3.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.2.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.1.1 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.1.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-17.0.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-16.29.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-16.28.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-16.27.1 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-16.27.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-16.26.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-16.25.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-16.24.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-16.23.0 node_modules/ajv-keywords/keywords/instanceof.js
govuk_publishing_components-16.22.0 node_modules/ajv-keywords/keywords/instanceof.js