Skip to content

Commit

Permalink
Added type and name expression properties #810 (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed Sep 30, 2021
1 parent 5cc4d4c commit 0548563
Show file tree
Hide file tree
Showing 23 changed files with 1,298 additions and 353 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -331,13 +331,15 @@ The following conceptual topics exist in the `PSRule` module:
- [Less](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#less)
- [LessOrEquals](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#lessorequals)
- [Match](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#match)
- [Name](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#name)
- [Not](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#not)
- [NotEquals](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#notequals)
- [NotIn](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#notin)
- [NotMatch](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#notmatch)
- [SetOf](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#setof)
- [StartsWith](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#startswith)
- [Subset](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#subset)
- [Type](docs/concepts/PSRule/en-US/about_PSRule_Expressions.md#type)
- [Options](docs/concepts/PSRule/en-US/about_PSRule_Options.md)
- [Binding.Field](docs/concepts/PSRule/en-US/about_PSRule_Options.md#bindingfield)
- [Binding.IgnoreCase](docs/concepts/PSRule/en-US/about_PSRule_Options.md#bindingignorecase)
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG-v1.md
Expand Up @@ -15,6 +15,10 @@ What's changed since pre-release v1.8.0-B2109015:
- General improvements:
- Added support for conditional reason messages with `ReasonIf`. [#804](https://github.com/microsoft/PSRule/issues/804)
- See [about_PSRule_Assert] for details.
- Added support for `type` and `name` expression properties. [#810](https://github.com/microsoft/PSRule/issues/810)
- Use `type` to compare the bound type of the current object.
- Use `name` to compare the bound name of the current object.
- See [about_PSRule_Expressions] for details.

## v1.8.0-B2109015 (pre-release)

Expand Down
78 changes: 78 additions & 0 deletions docs/concepts/PSRule/en-US/about_PSRule_Expressions.md
Expand Up @@ -44,6 +44,8 @@ The following operators are available:
The following comparison properties are available:

- [Field](#field)
- [Name](#name)
- [Type](#type)

### AllOf

Expand Down Expand Up @@ -762,6 +764,44 @@ spec:
match: '$(abc|efg)$'
```

### Name

The comparison property `name` is used with a condition to evaluate the target name of the object.
The `name` property must be set to `.`.
Any other value will cause the condition to evaluate to `false`.

Syntax:

```yaml
name: '.'
```

For example:

```yaml
---
apiVersion: github.com/microsoft/PSRule/v1
kind: Rule
metadata:
name: 'ExampleName'
spec:
condition:
name: '.'
equals: 'TargetObject1'

---
apiVersion: github.com/microsoft/PSRule/v1
kind: Selector
metadata:
name: 'ExampleName'
spec:
if:
name: '.'
in:
- 'TargetObject1'
- 'TargetObject2'
```

### Not

The `any` operator is used to invert the result of the nested expression.
Expand Down Expand Up @@ -1054,6 +1094,44 @@ spec:
unique: true
```

### Type

The comparison property `type` is used with a condition to evaluate the target type of the object.
The `type` property must be set to `.`.
Any other value will cause the condition to evaluate to `false`.

Syntax:

```yaml
type: '.'
```

For example:

```yaml
---
apiVersion: github.com/microsoft/PSRule/v1
kind: Rule
metadata:
name: 'ExampleType'
spec:
condition:
type: '.'
equals: 'CustomType'

---
apiVersion: github.com/microsoft/PSRule/v1
kind: Selector
metadata:
name: 'ExampleType'
spec:
if:
type: '.'
in:
- 'Microsoft.Storage/storageAccounts'
- 'Microsoft.Storage/storageAccounts/blobServices'
```

## NOTE

An online version of this document is available at https://microsoft.github.io/PSRule/concepts/PSRule/en-US/about_PSRule_Expressions.md.
Expand Down
11 changes: 7 additions & 4 deletions docs/concepts/PSRule/en-US/about_PSRule_Selectors.md
Expand Up @@ -44,15 +44,18 @@ Conditions and operators available for use include:
The following comparison properties are available:

- Field
- Name
- Type

To learn more about conditions, operators, and properties see [about_PSRule_Expressions](about_PSRule_Expressions.md).

Currently the following limitations apply:

- Selectors can only evaluate a field of the target object.
The following examples can not be evaluated by selectors:
- Bound properties such as `TargetName`, `TargetType`, and `Field`.
- State variables such as `$PSRule`.
- Selectors can evaluate:
- Fields of the target object.
- Type and name binding of the target object by using `name` and `type` comparison properties.
- State variables such has `$PSRule` can not be evaluated.
- Bound fields can not be evaluated.

### Using selectors as pre-conditions

Expand Down
100 changes: 72 additions & 28 deletions schemas/PSRule-language.schema.json
Expand Up @@ -571,20 +571,64 @@
"selectorProperties": {
"oneOf": [
{
"properties": {
"field": {
"type": "string",
"title": "Field",
"description": "The path of the field.",
"markdownDescription": "The path of the field. [See help](https://microsoft.github.io/PSRule/concepts/PSRule/en-US/about_PSRule_Selectors.html#field)"
}
},
"required": [
"field"
]
"$ref": "#/definitions/selectorPropertyField"
}
]
},
"selectorPropertiesString": {
"oneOf": [
{
"$ref": "#/definitions/selectorPropertyField"
},
{
"$ref": "#/definitions/selectorPropertyType"
},
{
"$ref": "#/definitions/selectorPropertyName"
}
]
},
"selectorPropertyField": {
"properties": {
"field": {
"type": "string",
"title": "Field",
"description": "The path of the field.",
"markdownDescription": "The path of the field. [See help](https://microsoft.github.io/PSRule/concepts/PSRule/en-US/about_PSRule_Selectors.html#field)"
}
},
"required": [
"field"
]
},
"selectorPropertyType": {
"properties": {
"type": {
"type": "string",
"title": "Type",
"description": "The target type of the object.",
"markdownDescription": "The target type of the object. [See help](https://microsoft.github.io/PSRule/concepts/PSRule/en-US/about_PSRule_Selectors.html#type)",
"default": "."
}
},
"required": [
"type"
]
},
"selectorPropertyName": {
"properties": {
"name": {
"type": "string",
"title": "Name",
"description": "The target name of the object.",
"markdownDescription": "The target name of the object. [See help](https://microsoft.github.io/PSRule/concepts/PSRule/en-US/about_PSRule_Selectors.html#name)",
"default": "."
}
},
"required": [
"name"
]
},
"selectorOperatorAllOf": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -688,7 +732,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand Down Expand Up @@ -723,7 +767,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -742,7 +786,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -761,7 +805,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -780,7 +824,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -799,7 +843,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -818,7 +862,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand Down Expand Up @@ -916,7 +960,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -935,7 +979,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -954,7 +998,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -973,7 +1017,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -992,7 +1036,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -1011,7 +1055,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -1030,7 +1074,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -1049,7 +1093,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -1068,7 +1112,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand All @@ -1087,7 +1131,7 @@
],
"oneOf": [
{
"$ref": "#/definitions/selectorProperties"
"$ref": "#/definitions/selectorPropertiesString"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Common/ExpressionHelpers.cs
Expand Up @@ -358,7 +358,7 @@ internal static bool AnyValue(object actualValue, object expectedValue, bool cas
{
foundValue = actualValue;
var expectedBase = GetBaseObject(expectedValue);
if (actualValue is IEnumerable items)
if (actualValue is IEnumerable items && !(actualValue is string))
{
foreach (var item in items)
{
Expand Down

0 comments on commit 0548563

Please sign in to comment.