Skip to content

Suggestion: Duck-typing ValidateProperty attribute #7835

@Jaykul

Description

@Jaykul

I'd like to propose adding a ValidateProperties attribute which would make it easier to validate that an incoming object had specific properties.

Hypothetically, use of it might look something like this:

[Parameter()]
[ValidateProperty(@{ SourceFile = [string]}, @{SourceLineNumber = [int]})]
[PSObject]$InputObject

The idea being that this would ensure that the InputObject has a SourceFile property of type string and a SourceLineNumber property of type int ...

Or you might even just do:

[Parameter()]
[ValidateProperty("SourceFile", "SourceLineNumber")]
[PSObject]$InputObject

Which would just validate the properties exist, and not their types.

We might even extend it with a NotNullOrEmpty check ...

Obviously I can add this in an external module, and I can do this validation with a ValidateScript ... but it seems like something that's generally useful (along the lines of the [PSTypeName()] attribute, but with the focus being on the shape of the object, rather than the name.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions