-
Notifications
You must be signed in to change notification settings - Fork 243
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
Add validation for keys in PickProperties #5868
base: main
Are you sure you want to change the base?
Add validation for keys in PickProperties #5868
Conversation
@microsoft-github-policy-service agree |
b65e1d8
to
279d57e
Compare
279d57e
to
6cd0012
Compare
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.
This seems like a good idea.
Could you run pnpm -w change add
to create a change record for this (and fill in a small message describing the change)? We use this to create release notes etc.
I have some comments on the implementation and some things that should be accounted for.
// Remove all properties not picked | ||
filterModelPropertiesInPlace(target, (prop) => pickedNames.has(prop.name)); | ||
}; | ||
|
||
function validatePropertyName(context: DecoratorContext, target: Model, name: string) { | ||
const source = target.templateMapper?.args[0] as Model; | ||
if (source && !source.properties?.has(name)) { |
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 think we need to account for and test for correct handling of inheritance here. The property might not be a property of the template argument itself, but one of its ancestors.
model X {
a: string;
}
model Y extends X {}
model Test is PickProperties<Y, "a">
{ | ||
code: "unexpected-property", | ||
message: | ||
"Object value may only specify known properties, and 'notMee' does not exist in type 'OriginalModel'.", |
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 don't think this is the right error message to give. We use this diagnostic internally when we are checking the assignability of objects, like this:
const a: {} = #{ b: "abc" };
Object value may only specify known properties, and 'a' does not exist in type '{}'.
I think we need to add a better/shorter message for this case. Something like
Property 'notMee' does not exist in type 'OriginalModel'.
propertyName: name, | ||
type: getTypeName(source), | ||
}, | ||
target: context.decoratorTarget, |
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.
We need to be careful here, because this will put the error on the invocation of the decorator, which is on the template. We would want to put it at least on the second template argument of the template, and best if we can put it specifically on the string that isn't a property.
Keys
Validation forPickProperties
Description
This PR introduces key validation for the pickProperties decorator in the TypeSpec compiler. The changes ensure that only valid keys are used when picking properties from a model. If an invalid key is provided, a diagnostic error will be emitted.
Changes
decorators.ts
file to include the validation checks.decorators.test.ts
to verify the new validation logic.Related Issues:
#4369