Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.defineType('ObjectId', (v) => {
return ObjectId.isValid(v) ? new ObjectId(v) : null;
});
}
const _safeObjectId = s => (ObjectId.isValid(s) ? new ObjectId(s) : null);
protected _base(v: any) {
if (ObjectId.isValid(v)) return null;
return "Must be a valid object id";
}
}
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(
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'
}
}
function parseObjectId (id) {
if (ObjectId.isValid(id)) {
return ObjectId(id)
}
throw new Error('ObjectId must be a single String of 24 hex characters')
}
export function stringToObjectIdOrString(id: string): string | ObjectId {
const valid = ObjectId.isValid(id);
if (valid) {
return ObjectId.createFromHexString(id);
}
return id;
}
export function isValidID(type, id) {
if (!id) {
return false;
}
if (id.type !== type) {
return false;
}
if (!ObjectId.isValid(id.value)) {
return false;
}
return true;
}
const _safeObjectId = s => (ObjectId.isValid(s) ? new ObjectId(s) : null);