-
Notifications
You must be signed in to change notification settings - Fork 939
Provide document path context for input validation errors. #3189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,6 +153,8 @@ interface ContextSettings { | |
readonly dataSource: UserDataSource; | ||
/** The name of the method the user called to create the ParseContext. */ | ||
readonly methodName: string; | ||
/** The document the user is attempting to modify, if that applies. */ | ||
readonly targetDoc?: DocumentKey; | ||
/** | ||
* A path within the object being parsed. This could be an empty path (in | ||
* which case the context represents the root of the data being parsed), or a | ||
|
@@ -247,15 +249,11 @@ export class ParseContext { | |
} | ||
|
||
createError(reason: string): Error { | ||
const fieldDescription = | ||
!this.path || this.path.isEmpty() | ||
? '' | ||
: ` (found in field ${this.path.toString()})`; | ||
return new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
`Function ${this.settings.methodName}() called with invalid data. ` + | ||
reason + | ||
fieldDescription | ||
return createError( | ||
reason, | ||
this.settings.methodName, | ||
this.path, | ||
this.settings.targetDoc | ||
); | ||
} | ||
|
||
|
@@ -309,14 +307,16 @@ export class UserDataReader { | |
/** Parse document data from a set() call. */ | ||
parseSetData( | ||
methodName: string, | ||
targetDoc: DocumentKey, | ||
input: unknown, | ||
options: firestore.SetOptions = {} | ||
): ParsedSetData { | ||
const context = this.createContext( | ||
options.merge || options.mergeFields | ||
? UserDataSource.MergeSet | ||
: UserDataSource.Set, | ||
methodName | ||
methodName, | ||
targetDoc | ||
); | ||
validatePlainObject('Data must be an object, but it was:', context, input); | ||
const updateData = parseObject(input, context)!; | ||
|
@@ -338,7 +338,8 @@ export class UserDataReader { | |
} else if (typeof stringOrFieldPath === 'string') { | ||
fieldPath = fieldPathFromDotSeparatedString( | ||
methodName, | ||
stringOrFieldPath | ||
stringOrFieldPath, | ||
targetDoc | ||
); | ||
} else { | ||
throw fail( | ||
|
@@ -375,14 +376,22 @@ export class UserDataReader { | |
} | ||
|
||
/** Parse update data from an update() call. */ | ||
parseUpdateData(methodName: string, input: unknown): ParsedUpdateData { | ||
const context = this.createContext(UserDataSource.Update, methodName); | ||
parseUpdateData( | ||
methodName: string, | ||
targetDoc: DocumentKey, | ||
input: unknown | ||
): ParsedUpdateData { | ||
const context = this.createContext( | ||
UserDataSource.Update, | ||
methodName, | ||
targetDoc | ||
); | ||
validatePlainObject('Data must be an object, but it was:', context, input); | ||
|
||
const fieldMaskPaths: FieldPath[] = []; | ||
const updateData = new ObjectValueBuilder(); | ||
forEach(input as Dict<unknown>, (key, value) => { | ||
const path = fieldPathFromDotSeparatedString(methodName, key); | ||
const path = fieldPathFromDotSeparatedString(methodName, key, targetDoc); | ||
|
||
const childContext = context.childContextForFieldPath(path); | ||
if ( | ||
|
@@ -411,12 +420,17 @@ export class UserDataReader { | |
/** Parse update data from a list of field/value arguments. */ | ||
parseUpdateVarargs( | ||
methodName: string, | ||
targetDoc: DocumentKey, | ||
field: string | BaseFieldPath, | ||
value: unknown, | ||
moreFieldsAndValues: unknown[] | ||
): ParsedUpdateData { | ||
const context = this.createContext(UserDataSource.Update, methodName); | ||
const keys = [fieldPathFromArgument(methodName, field)]; | ||
const context = this.createContext( | ||
UserDataSource.Update, | ||
methodName, | ||
targetDoc | ||
); | ||
const keys = [fieldPathFromArgument(methodName, field, targetDoc)]; | ||
const values = [value]; | ||
|
||
if (moreFieldsAndValues.length % 2 !== 0) { | ||
|
@@ -474,12 +488,14 @@ export class UserDataReader { | |
/** Creates a new top-level parse context. */ | ||
private createContext( | ||
dataSource: UserDataSource, | ||
methodName: string | ||
methodName: string, | ||
targetDoc?: DocumentKey | ||
): ParseContext { | ||
return new ParseContext( | ||
{ | ||
dataSource, | ||
methodName, | ||
targetDoc, | ||
path: FieldPath.EMPTY_PATH, | ||
arrayElement: false | ||
}, | ||
|
@@ -740,18 +756,16 @@ function validatePlainObject( | |
*/ | ||
export function fieldPathFromArgument( | ||
methodName: string, | ||
path: string | BaseFieldPath | ||
path: string | BaseFieldPath, | ||
targetDoc?: DocumentKey | ||
): FieldPath { | ||
if (path instanceof BaseFieldPath) { | ||
return path._internalPath; | ||
} else if (typeof path === 'string') { | ||
return fieldPathFromDotSeparatedString(methodName, path); | ||
} else { | ||
const message = 'Field path arguments must be of type string or FieldPath.'; | ||
throw new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
`Function ${methodName}() called with invalid data. ${message}` | ||
); | ||
throw createError(message, methodName, undefined, targetDoc); | ||
} | ||
} | ||
|
||
|
@@ -761,22 +775,49 @@ export function fieldPathFromArgument( | |
* @param methodName The publicly visible method name | ||
* @param path The dot-separated string form of a field path which will be split | ||
* on dots. | ||
* @param targetDoc The document against which the field path will be evaluated. | ||
*/ | ||
export function fieldPathFromDotSeparatedString( | ||
methodName: string, | ||
path: string | ||
path: string, | ||
targetDoc?: DocumentKey | ||
): FieldPath { | ||
try { | ||
return fromDotSeparatedString(path)._internalPath; | ||
} catch (e) { | ||
const message = errorMessage(e); | ||
throw new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
`Function ${methodName}() called with invalid data. ${message}` | ||
); | ||
throw createError(message, methodName, undefined, targetDoc); | ||
} | ||
} | ||
|
||
function createError( | ||
reason: string, | ||
methodName: string, | ||
path?: FieldPath, | ||
targetDoc?: DocumentKey | ||
): Error { | ||
const hasPath = path && !path.isEmpty(); | ||
const hasDocument = targetDoc !== undefined; | ||
|
||
let description = ''; | ||
if (hasPath || hasDocument) { | ||
description += ' (found'; | ||
|
||
if (hasPath) { | ||
description += ` in field ${path!.toString()}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
} | ||
if (hasDocument) { | ||
description += ` in document ${targetDoc!.toString()}`; | ||
} | ||
description += ')'; | ||
} | ||
|
||
return new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
`Function ${methodName}() called with invalid data. ` + reason + description | ||
); | ||
} | ||
|
||
/** | ||
* Extracts the message from a caught exception, which should be an Error object | ||
* though JS doesn't guarantee that. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be time to add a helper method to ParseContext (or a tree-shakeable function in this file) that simplifies the creation of an argument parser. If
arrayElement
remains configurable, this code would replace line 112-122, 145-155 and 173-181.Optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pulled out
createSentinelChildContext
. LMK if this is what you had in mind.