-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: relax constrain check to allow input containing constrained values #2754
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,10 @@ | |
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {Filter, WhereBuilder, Where, FilterBuilder} from '../query'; | ||
| import {AnyObject, DataObject} from '../common-types'; | ||
| import {cloneDeep} from 'lodash'; | ||
| import {AnyObject, DataObject} from '../common-types'; | ||
| import {Entity} from '../model'; | ||
| import {Filter, FilterBuilder, Where, WhereBuilder} from '../query'; | ||
|
|
||
| /** | ||
| * A utility function which takes a filter and enforces constraint(s) | ||
|
|
@@ -55,12 +55,16 @@ export function constrainDataObject<T extends Entity>( | |
| ): DataObject<T> { | ||
| const constrainedData = cloneDeep(originalData); | ||
| for (const c in constraint) { | ||
| if (constrainedData.hasOwnProperty(c)) | ||
| if (constrainedData.hasOwnProperty(c)) { | ||
| // Known limitation: === does not work for objects such as ObjectId | ||
| if (originalData[c] === constraint[c]) continue; | ||
| throw new Error(`Property "${c}" cannot be changed!`); | ||
| } | ||
| (constrainedData as AnyObject)[c] = constraint[c]; | ||
| } | ||
| return constrainedData; | ||
| } | ||
|
|
||
| /** | ||
| * A utility function which takes an array of model instance data and | ||
| * enforces constraint(s) on it | ||
|
|
@@ -71,13 +75,7 @@ export function constrainDataObject<T extends Entity>( | |
| */ | ||
| export function constrainDataObjects<T extends Entity>( | ||
| originalData: DataObject<T>[], | ||
| constraint: Partial<T>, | ||
| constraint: DataObject<T>, | ||
| ): DataObject<T>[] { | ||
| const constrainedData = cloneDeep(originalData); | ||
| for (let obj of constrainedData) { | ||
| for (let prop in constraint) { | ||
| (obj as AnyObject)[prop] = constraint[prop]; | ||
| } | ||
| } | ||
| return constrainedData; | ||
| return originalData.map(obj => constrainDataObject(obj, constraint)); | ||
|
Member
Author
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. This is slightly changing the behavior when the input data is trying to modify the constrained properties. Before my change, we would silently replace values provided by the user with values enforced by the constraint. With my change in place, we reject requests trying to modify constrained properties. AFAICT, the function |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.