Skip to content

Add validation for keys in PickProperties #5868

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
---

Keys Validation for PickProperties
1 change: 1 addition & 0 deletions packages/compiler/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ const diagnostics = {
severity: "error",
messages: {
default: paramMessage`Object value may only specify known properties, and '${"propertyName"}' does not exist in type '${"type"}'.`,
typeMissingProperty: paramMessage`Property '${"propertyName"}' does not exist in type '${"type"}'.`,
},
},
"extends-interface": {
Expand Down
34 changes: 34 additions & 0 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import {
DiagnosticTarget,
Enum,
EnumValue,
IndeterminateEntity,
Interface,
Model,
ModelProperty,
Expand Down Expand Up @@ -937,10 +938,43 @@ export const $withPickedProperties: WithPickedPropertiesDecorator = (
}
}

// Validate that all picked properties exist
for (const name of pickedNames) {
validatePropertyName(context, target, name);
}

// 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;
const entity = target.templateMapper?.args[1];
if (source && entity && !target.properties.has(name)) {
reportDiagnostic(context.program, {
code: "unexpected-property",
format: {
propertyName: name,
type: getTypeName(source),
},
messageId: "typeMissingProperty",
target: getExactDiagnosticTarget(entity, name),
});
}
}

function getExactDiagnosticTarget(
entity: Type | Value | IndeterminateEntity,
name: string,
): DiagnosticTarget {
if ("kind" in entity && entity.kind === "Union") {
return (
entity.node.options.find((option) => "value" in option && option.value === name) ?? entity
);
}
return entity;
}

// -- @withoutDefaultValues decorator ----------------------

export const $withoutDefaultValues: WithoutDefaultValuesDecorator = (
Expand Down
44 changes: 44 additions & 0 deletions packages/compiler/test/decorators/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,50 @@ describe("compiler: built-in decorators", () => {
const properties = TestModel.kind === "Model" ? Array.from(TestModel.properties.keys()) : [];
deepStrictEqual(properties, ["pickMe", "pickMeToo"]);
});

it("picks model inherited property", async () => {
const { TestModel } = await runner.compile(
`
model ParentModel {
pickMe: string;
}

model OriginalModel extends ParentModel {
pickMeToo: string;
notMe: string;
}

@test
model TestModel is PickProperties<OriginalModel, "pickMe"> {
}`,
);

const properties = TestModel.kind === "Model" ? Array.from(TestModel.properties.keys()) : [];
deepStrictEqual(properties, ["pickMe"]);
});

it("emits diagnostics if any given key is not a property of a model", async () => {
const diagnostics = await runner.diagnose(
`
model OriginalModel {
pickMe: string;
pickMeToo: string;
notMe: string;
}

model TestModel is PickProperties<OriginalModel, "pickMe" | "notMee"> {
}`,
);

expectDiagnostics(diagnostics, [
{
code: "unexpected-property",
message: "Property 'notMee' does not exist in type 'OriginalModel'.",
pos: 190,
end: 198,
},
]);
});
});

describe("@overload", () => {
Expand Down