Ejemplo n.º 1
0
	test('should return true when value is Arguments, Function, String, Number, Date, RegExp', function () {
		var stringVar = 'string';
		var functionVar = function () {
		};
		var numberVar = 10;
		var dateVar = new Date();
		var erVar = /[a-z]/;
		var expectArguments = function () {
			assert.isTrue(util.isArguments(arguments));
		};
		var objVar = {};

		assert.isTrue(util.isString(stringVar));
		assert.isTrue(util.isFunction(functionVar));
		assert.isTrue(util.isNumber(numberVar));
		assert.isTrue(util.isDate(dateVar));
		assert.isTrue(util.isRegExp(erVar));
		assert.isTrue(util.isObject(objVar));
		expectArguments();
	});
Ejemplo n.º 2
0
const isEmptyObjectOrArray = (obj: any): boolean => {
  if (obj === INVALID_VALUE) {
    return true
  } else if (_.isDate(obj)) {
    return false
    // Simple "is object empty" check.
  } else if (_.isObject(obj) && _.isEmpty(obj)) {
    return true
  } else if (_.isObject(obj)) {
    return _.every(obj, (value, key) => {
      if (!isDefined(value)) {
        return true
      } else if (_.isObject(value)) {
        return isEmptyObjectOrArray(value)
      } else {
        return false
      }
    })
  }
  return false
}
Ejemplo n.º 3
0
var isEmptyObjectOrArray = function isEmptyObjectOrArray(obj) {
  if (obj === INVALID_VALUE) {
    return true;
  } else if (_.isDate(obj)) {
    return false;
    // Simple "is object empty" check.
  } else if (_.isObject(obj) && _.isEmpty(obj)) {
    return true;
  } else if (_.isObject(obj)) {
    return _.every(obj, function (value, key) {
      if (!isDefined(value)) {
        return true;
      } else if (_.isObject(value)) {
        return isEmptyObjectOrArray(value);
      } else {
        return false;
      }
    });
  }
  return false;
};
Ejemplo n.º 4
0
function parse(schema, prefix, options, model) {
  var result;

  if ((prefix === '') && (type.isObject(schema) === false) && (util.isPlainObject(schema) === false)) {
    throw new Errors.ValidationError("The schema must be a plain object.")
  }

  // Validate a schema and add the field _enum if needed
  if (util.isPlainObject(schema)) {
    if (schema._type !== undefined) {
      options = util.mergeOptions(options, schema.options);
      var result;
      switch(schema._type) {
        case String:
          result = type.string().options(options).validator(schema.validator).enum(schema.enum);
          if (schema.default !== undefined) { result.default(schema.default); }
          if (typeof schema.min === "number") { result.min(schema.min); }
          if (typeof schema.max === "number") { result.max(schema.max); }
          if (typeof schema.length === "number") { result.length(schema.length); }
          if (schema.alphanum === true) { result.alphanum(); }
          if (schema.lowercase === true) { result.lowercase(); }
          if (schema.uppercase === true) { result.uppercase(); }
          if (typeof schema.regex === "string") { result.regex(regex, schema.flags); }
          return result;
        case Number:
          result = type.number().options(options).validator(schema.validator);
          if (schema.default !== undefined) { result.default(schema.default); }
          if (typeof schema.min === "number") { result.min(schema.min); }
          if (typeof schema.max === "number") { result.max(schema.max); }
          if (typeof schema.length === "number") { result.length(schema.length); }
          if (schema.integer === true) { result.integer(); }
          return result;
        case Boolean:
          result = type.boolean().options(options).validator(schema.validator);
          if (schema.default !== undefined) { result.default(schema.default); }
          return result;
        case Date:
          var result = type.date().options(options).validator(schema.validator);
          if (schema.default !== undefined) { result.default(schema.default); }
          if (schema.min instanceof Date) { result.min(schema.min); }
          if (schema.max instanceof Date) { result.max(schema.max); }
          return result;
        case Buffer:
          result = type.buffer().options(options).validator(schema.validator);
          if (schema.default !== undefined) { result.default(schema.default); }
          return result
        case Object:
          result = type.object().options(options).validator(schema.validator);
          if (schema.default !== undefined) { result.default(schema.default); }
          util.loopKeys(schema.schema, function(_schema, key) {
            result.setKey(key, parse(_schema[key], prefix+"["+key+"]", options));
          })
          if (prefix === '') {
            result._setModel(model)
          }
          return result;
        case Array:
          var result = type.array().options(options).validator(schema.validator);
          if (schema.default !== undefined) { result.default(schema.default); }
          if (schema.schema !== undefined) {
            result.schema(parse(schema.schema, prefix+"[0]", options));
          }
          if (typeof schema.min === "number") { result.min(schema.min); }
          if (typeof schema.max === "number") { result.max(schema.max); }
          if (typeof schema.length === "number") { result.length(schema.length); }
          return result;
        case 'Point':
          result = type.point().options(options).validator(schema.validator);
          if (schema.default !== undefined) { result.default(schema.default); }
          return result;
        case 'virtual':
          result = type.virtual();
          if (schema.default !== undefined) { result.default(schema.default); }
          return result
        default: // Unknown type
          throw new Errors.ValidationError("The field `_type` must be `String`/`Number`/`Boolean`/`Date`/`Buffer`/`Object`/`Array`/`'virtual'`/`'Point'` for "+prefix);
      }
    }
    else if (type.isString(schema)
        || type.isNumber(schema)
        || type.isBoolean(schema)
        || type.isDate(schema)
        || type.isBuffer(schema)
        || type.isPoint(schema)
        || type.isObject(schema)
        || type.isArray(schema)
        || type.isAny(schema)
        || type.isVirtual(schema)){ // Unknown type
      // Nothing to do here
      if (type.isObject(schema)) {
        parse(schema._schema, prefix, options);
      }
      else if (type.isArray(schema)) {
        if (schema._schema == undefined) {
          schema._schema = parse(type.any(), prefix, options);
        }
        else {
          schema._schema = parse(schema._schema, prefix, options);
        }
      }

      // We want to copy the model object here
      if (util.isPlainObject(schema._options) === false) {
        schema.options(options);
      }
      else if ((schema._options.enforce_extra === undefined)
          || (schema._options.enforce_missing === undefined)
          || (schema._options.enforce_type === undefined)) {
        var newOptions = {};
        newOptions.enforce_missing = (schema._options.enforce_missing != null) ? schema._options.enforce_missing : options.enforce_missing;
        newOptions.enforce_extra = (schema._options.enforce_extra != null) ? schema._options.enforce_extra : options.enforce_extra;
        newOptions.enforce_type = (schema._options.enforce_type != null) ? schema._options.enforce_type : options.enforce_type;
        schema.options(newOptions);
      }
      return schema;
    }
    else {
      result = type.object().options(options);
      util.loopKeys(schema, function(_schema, key) {
        result.setKey(key, parse(_schema[key], prefix+"["+key+"]", options));
      })
      if (prefix === '') {
        result._setModel(model)
      }
      return result;
    }
  }
  else if (Array.isArray(schema)) {
    result = type.array().options(options);
    if (schema.length > 1) {
      throw new Errors.ValidationError("An array in a schema can have at most one element. Found "+schema.length+" elements in "+prefix)
    }

    if (schema.length > 0) {
      result.schema(parse(schema[0], prefix+"[0]", options));
    }
    return result;

  }
  else if (schema === String) {
    return type.string().options(options);
  }
  else if (schema === Number) {
    return type.number().options(options);
  }
  else if (schema === Boolean) {
    return type.boolean().options(options);
  }
  else if (schema === Date) {
    return type.date().options(options);
  }
  else if (schema === Buffer) {
    return type.buffer().options(options);
  }
  else if (schema === Object) {
    return type.object().options(options);
  }
  else if (schema === Array) {
    return type.array().options(options);
  }
  else if (schema === 'Point') {
    return type.point().options(options);
  }
  else if (schema === 'virtual') {
    return type.virtual().options(options);
  }
  else {
    throw new Errors.ValidationError("The value must be `String`/`Number`/`Boolean`/`Date`/`Buffer`/`Object`/`Array`/`'virtual'`/`'Point'` for "+prefix);
  }
}
Ejemplo n.º 5
0
'use strict';

const common = require('../../common');

// This tests the date-related n-api calls

const assert = require('assert');
const test_date = require(`./build/${common.buildType}/test_date`);

const dateTypeTestDate = test_date.createDate(1549183351);
assert.strictEqual(test_date.isDate(dateTypeTestDate), true);

assert.strictEqual(test_date.isDate(new Date(1549183351)), true);

assert.strictEqual(test_date.isDate(2.4), false);
assert.strictEqual(test_date.isDate('not a date'), false);
assert.strictEqual(test_date.isDate(undefined), false);
assert.strictEqual(test_date.isDate(null), false);
assert.strictEqual(test_date.isDate({}), false);

assert.strictEqual(test_date.getDateValue(new Date(1549183351)), 1549183351);
Ejemplo n.º 6
0
 dateString => {
   expect(isDate(dateString)).toBeFalsy()
 }
Ejemplo n.º 7
0
 ])(`should return true for nanosecond precision: %s`, dateString => {
   expect(isDate(dateString)).toBeTruthy()
 })
Ejemplo n.º 8
0
 dateString => {
   expect(isDate(dateString)).toBeTruthy()
 }
Ejemplo n.º 9
0
 ])(`should return true for ISO 8601 (no T, extra space): %s`, dateString => {
   expect(isDate(dateString)).toBeTruthy()
 })
Ejemplo n.º 10
0
 ])(`should return true for valid ISO 8601: %s`, dateString => {
   expect(isDate(dateString)).toBeTruthy()
 })
Ejemplo n.º 11
0
 ])(`should return true for unix timestamps: %s`, dateString => {
   expect(isDate(dateString)).toBeTruthy()
 })
Ejemplo n.º 12
0
 ])(`should return false for invalid ISO 8601: %s`, dateString => {
   expect(isDate(dateString)).toBeFalsy()
 })
Ejemplo n.º 13
0
const getSimpleFieldConfig = ({
  schemaComposer,
  typeComposer,
  nodeStore,
  key,
  value,
  selector,
  typeMapping,
  addNewFields,
  addDefaultResolvers,
}) => {
  switch (typeof value) {
    case `boolean`:
      return { type: `Boolean` }
    case `number`:
      return { type: is32BitInteger(value) ? `Int` : `Float` }
    case `string`:
      if (isDate(value)) {
        return dateResolver
      }
      // FIXME: The weird thing is that we are trying to infer a File,
      // but cannot assume that a source plugin for File nodes is actually present.
      if (schemaComposer.has(`File`) && isFile(nodeStore, selector, value)) {
        // NOTE: For arrays of files, where not every path references
        // a File node in the db, it is semi-random if the field is
        // inferred as File or String, since the exampleValue only has
        // the first entry (which could point to an existing file or not).
        return { type: `File`, resolve: fileByPath }
      }
      return { type: `String` }
    case `object`:
      if (value instanceof Date) {
        return dateResolver
      }
      if (value instanceof String) {
        return { type: `String` }
      }
      if (value /* && depth < MAX_DEPTH*/) {
        // We only create a temporary TypeComposer on nested fields
        // (either a clone of an existing field type, or a temporary new one),
        // because we don't yet know if this type should end up in the schema.
        // It might be for a possibleField that will be disregarded later,
        // so we cannot mutate the original.
        let fieldTypeComposer
        if (
          typeComposer.hasField(key) &&
          getNamedType(typeComposer.getFieldType(key)) instanceof
            GraphQLObjectType
        ) {
          const originalFieldTypeComposer = typeComposer.getFieldTC(key)
          fieldTypeComposer = originalFieldTypeComposer.clone(
            originalFieldTypeComposer.getTypeName()
          )
        } else {
          fieldTypeComposer = ObjectTypeComposer.createTemp(
            createTypeName(selector),
            schemaComposer
          )
        }

        return {
          type: addInferredFieldsImpl({
            schemaComposer,
            typeComposer: fieldTypeComposer,
            nodeStore,
            exampleObject: value,
            typeMapping,
            prefix: selector,
            addNewFields,
            addDefaultResolvers,
          }),
        }
      }
  }
  throw new Error(`Can't determine type for "${value}" in \`${selector}\`.`)
}