-
Notifications
You must be signed in to change notification settings - Fork 31
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
validateAssetMeta merges any existing asset meta #1233
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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.
You should also add some unit tests for this feature as I believe you have not exercised the code to make sure it works (right now it does not).
The tests would go around here (as separate it
s): https://github.com/livepeer/livepeer-com/blob/845e074c9f755e876bb23db83b374901d4c68842/packages/api/src/controllers/asset.test.ts#L218
You should add some cases like:
- Allows adding
meta
to an asset - Allows adding fields to an existing
meta
in anasset
- Allows removing a filed from the
meta
by setting it tonull
- Disallows any update that would result in a meta greater than that
META_MAX_SIZE
(incl if it only hits max size after the merge)
@@ -47,7 +47,10 @@ const app = Router(); | |||
|
|||
const META_MAX_SIZE = 1024; | |||
|
|||
function validateAssetMeta(meta: Record<string, string>) { | |||
function validateAssetMeta( meta: Record<string, string>, asset?:WithID<Asset> ) { |
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.
Apparently the prettier is still not working on your env. Run yarn prettier
manually from the project root instead.
@@ -47,7 +47,10 @@ const app = Router(); | |||
|
|||
const META_MAX_SIZE = 1024; | |||
|
|||
function validateAssetMeta(meta: Record<string, string>) { | |||
function validateAssetMeta( meta: Record<string, string>, asset?:WithID<Asset> ) { | |||
meta = _({ ...asset.meta, ...meta }) |
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.
The asset is not always available so this needs to be ?.
meta = _({ ...asset.meta, ...meta }) | |
meta = _({ ...asset?.meta, ...meta }) |
function validateAssetMeta(meta: Record<string, string>) { | ||
function validateAssetMeta( meta: Record<string, string>, asset?:WithID<Asset> ) { | ||
meta = _({ ...asset.meta, ...meta }) | ||
.omit(_.isNull) |
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.
if (!asset || (asset.userId !== req.user.id && !req.user.admin)) { | ||
throw new NotFoundError(`asset not found`); | ||
} else if (asset.status.phase !== "ready") { | ||
throw new UnprocessableEntityError(`asset is not ready`); | ||
} | ||
validateAssetMeta(meta, asset); |
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.
The return value of this function is being ignored. It is also not the only place where this is called. Anytime you change the behavior of a function you need to update everywhere it is used. So keep that in mind and look for the other usages in the code.
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.
Actually... This function is already being called in this API handler. You should call it only once. No reason to call it again. Remove this line and edit the original call instead.
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.
LGTM!
What does this pull request do? Explain your changes. (required)
Addressing the issue #1229
Specific updates (required)
Change validateAssetMeta function to merge with any existing meta