How to use the mongodb.ObjectId.isValid function in mongodb

To help you get started, we’ve selected a few mongodb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rawmodel / framework / tests / performance.ts View on Github external
this.defineType('ObjectId', (v) => {
      return ObjectId.isValid(v) ? new ObjectId(v) : null;
    });
  }
github mgm-interns / team-radio / backend / src / models / user.js View on Github external
const _safeObjectId = s => (ObjectId.isValid(s) ? new ObjectId(s) : null);
github foxifyjs / odin / src / types / Id.ts View on Github external
protected _base(v: any) {
    if (ObjectId.isValid(v)) return null;

    return "Must be a valid object id";
  }
}
github reindexio / reindex-api / db / mongodb / queries / connectionQueries.js View on Github external
export function getConnectionQueries(
  db,
  type,
  filter = [],
  args = {},
  context,
) {
  if (args.before && !ObjectId.isValid(args.before.value)) {
    throw new UserError('Invalid `before` cursor');
  }

  if (args.after && !ObjectId.isValid(args.after.value)) {
    throw new UserError('Invalid `after` cursor');
  }

  const unsortableKeys = new Set(
    context.typeRegistry
      .getTypeSet(type)
      .getFilters()
      .filter((field) => field.type === 'List')
      .map((field) => field.name)
  );

  return getPaginatedQuery(
github foxifyjs / foxify / src / database / types / ObjectId.ts View on Github external
protected _base(v: any) {
    if (
      (String.isInstance(v) || Number.isInstance(v) || Function.isInstance(v)) &&
      ObjectId.isValid(v)
    ) return null

    return 'Must be an object id'
  }
}
github jo0ger / node-server / src / config / graphql / graphql.service.ts View on Github external
function parseObjectId (id) {
  if (ObjectId.isValid(id)) {
    return ObjectId(id)
  }
  throw new Error('ObjectId must be a single String of 24 hex characters')
}
github mediathekview / mediathekviewweb / server / src / repository / mongo / utils.ts View on Github external
export function stringToObjectIdOrString(id: string): string | ObjectId {
  const valid = ObjectId.isValid(id);

  if (valid) {
    return ObjectId.createFromHexString(id);
  }

  return id;
}
github reindexio / reindex-api / db / mongodb / queries / queryUtils.js View on Github external
export function isValidID(type, id) {
  if (!id) {
    return false;
  }
  if (id.type !== type) {
    return false;
  }
  if (!ObjectId.isValid(id.value)) {
    return false;
  }
  return true;
}
github mgm-interns / team-radio / backend / src / models / score-log.js View on Github external
const _safeObjectId = s => (ObjectId.isValid(s) ? new ObjectId(s) : null);