Skip to content

Commit

Permalink
Merge branch 'mrschmidt-validateuserinput' into mrschmidt-cleanupall
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Dec 28, 2018
2 parents 37703df + 1140b87 commit 094ec19
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions dev/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,72 @@ follow these steps, YOUR APP MAY BREAK.`);
}
}

/**
* Validates a JavaScript object for usage as a Firestore document.
*
* @private
* @param obj JavaScript object to validate.
* @param options Validation options
* @returns 'true' when the object is valid.
* @throws when the object is invalid.
*/
function validateDocumentData(
obj: DocumentData, options: ValidationOptions): boolean {
if (!isPlainObject(obj)) {
throw customObjectError(obj);
}

options = options || {};

let isEmpty = true;

for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
isEmpty = false;
validateFieldValue(obj[prop], options, new FieldPath(prop));
}
}

if (options.allowEmpty === false && isEmpty) {
throw new Error('At least one field must be updated.');
}

return true;
}

/**
* Validates the use of 'options' as ReadOptions and enforces that 'fieldMask'
* is an array of strings or field paths.
*
* @private
* @param options.fieldMask - The subset of fields to return from a read
* operation.
*/
export function validateReadOptions(options: ReadOptions): boolean {
if (!is.object(options)) {
throw new Error('Input is not an object.');
}

if (options.fieldMask === undefined) {
return true;
}

if (!Array.isArray(options.fieldMask)) {
throw new Error('"fieldMask" is not an array.');
}

for (let i = 0; i < options.fieldMask.length; ++i) {
try {
FieldPath.validateFieldPath(options.fieldMask[i]);
} catch (err) {
throw new Error(
`Element at index ${i} is not a valid FieldPath. ${err.message}`);
}
}

return true;
}

/**
* A logging function that takes a single string.
*
Expand Down

0 comments on commit 094ec19

Please sign in to comment.