Skip to content
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

Added sub-selectors for YAML and JSON #1024 #1045 #1238

Merged
merged 1 commit into from
Aug 26, 2022
Merged
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
2 changes: 1 addition & 1 deletion .markdownlint.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"no-reversed-links": true,
"no-multiple-blanks": true,
"line-length": {
"line_length": 100,
"line_length": 120,
"code_blocks": false,
"tables": false,
"headers": true
Expand Down
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
"./schemas/PSRule-language.schema.json": [
"/tests/PSRule.Tests/**.Rule.yaml",
"/tests/PSRule.Tests/**/**.Rule.yaml",
"/docs/scenarios/*/*.Rule.yaml"
"/docs/scenarios/*/*.Rule.yaml",
"/docs/expressions/**/*.Rule.yaml"
]
},
"json.schemas": [
{
"fileMatch": [
"/tests/PSRule.Tests/**.Rule.jsonc",
"/tests/PSRule.Tests/**/**.Rule.jsonc",
"/docs/expressions/**/*.Rule.jsonc"
],
"url": "./schemas/PSRule-resources.schema.json"
}
Expand Down
19 changes: 18 additions & 1 deletion docs/CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,26 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers

**Experimental features**:

- Functions within YAML expressions can be used to perform manipulation prior to testing a condition.
- Functions within YAML and JSON expressions can be used to perform manipulation prior to testing a condition.
See [functions][3] for more information.
- Sub-selectors within YAML and JSON expressions can be used to filter rules and list properties.
See [sub-selectors][4] for more information.

[3]: expressions/functions.md
[4]: expressions/sub-selectors.md

## Unreleased

What's changed since pre-release v2.4.0-B0039:

- New features:
- **Experimental**: Added support for sub-selectors YAML and JSON expressions by @BernieWhite.
[#1024](https://github.com/microsoft/PSRule/issues/1024)
[#1045](https://github.com/microsoft/PSRule/issues/1045)
- Sub-selector pre-conditions add an additional expression to determine if a rule is executed.
- Sub-selector object filters provide an way to filter items from list properties.
- See [sub-selectors][4] for more information.

## v2.4.0-B0039 (pre-release)

What's changed since pre-release v2.4.0-B0022:
Expand All @@ -27,6 +43,7 @@ What's changed since pre-release v2.4.0-B0022:
- Added conversion functions `boolean`, `string`, and `integer`.
- Added lookup functions `configuration`, and `path`.
- Added string functions `concat`, `substring`.
- See [functions][3] for more information.
- Bug fixes:
- Fixed reporting of duplicate identifiers which were not generating an error for all cases by @BernieWhite.
[#1229](https://github.com/microsoft/PSRule/issues/1229)
Expand Down
74 changes: 74 additions & 0 deletions docs/expressions/SubSelectors.Rule.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[
{
// Synopsis: A rule with a sub-selector precondition.
"apiVersion": "github.com/microsoft/PSRule/v1",
"kind": "Rule",
"metadata": {
"name": "Json.Subselector.Precondition"
},
"spec": {
"where": {
"field": "kind",
"equals": "api"
},
"condition": {
"field": "resources",
"count": 10
}
}
},
{
// Synopsis: A rule with a sub-selector filter.
"apiVersion": "github.com/microsoft/PSRule/v1",
"kind": "Rule",
"metadata": {
"name": "Json.Subselector.Filter"
},
"spec": {
"condition": {
"field": "resources",
"where": {
"type": ".",
"equals": "Microsoft.Web/sites/config"
},
"allOf": [
{
"field": "properties.detailedErrorLoggingEnabled",
"equals": true
}
]
}
}
},
{
// Synopsis: A rule with a sub-selector filter.
"apiVersion": "github.com/microsoft/PSRule/v1",
"kind": "Rule",
"metadata": {
"name": "Json.Subselector.FilterOr"
},
"spec": {
"condition": {
"anyOf": [
{
"field": "resources",
"where": {
"type": ".",
"equals": "Microsoft.Web/sites/config"
},
"allOf": [
{
"field": "properties.detailedErrorLoggingEnabled",
"equals": true
}
]
},
{
"field": "resources",
"exists": false
}
]
}
}
}
]
57 changes: 57 additions & 0 deletions docs/expressions/SubSelectors.Rule.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

#
# YAML-based rules for documentation
#

---
# Synopsis: A rule with a sub-selector precondition.
apiVersion: github.com/microsoft/PSRule/v1
kind: Rule
metadata:
name: Yaml.Subselector.Precondition
spec:
where:
field: 'kind'
equals: 'api'
condition:
field: resources
count: 10

---
# Synopsis: A rule with a sub-selector filter.
apiVersion: github.com/microsoft/PSRule/v1
kind: Rule
metadata:
name: Yaml.Subselector.Filter
spec:
condition:
field: resources
where:
type: '.'
equals: 'Microsoft.Web/sites/config'
allOf:
- field: properties.detailedErrorLoggingEnabled
equals: true

---
# Synopsis: A rule with a sub-selector filter.
apiVersion: github.com/microsoft/PSRule/v1
kind: Rule
metadata:
name: Yaml.Subselector.FilterOr
spec:
condition:
anyOf:

- field: resources
where:
type: '.'
equals: 'Microsoft.Web/sites/config'
allOf:
- field: properties.detailedErrorLoggingEnabled
equals: true

- field: resources
exists: false
12 changes: 9 additions & 3 deletions docs/expressions/functions.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# Expression functions
# Functions

!!! Abstract
Functions are an advanced lanaguage feature specific to YAML and JSON resources.
_Functions_ are an advanced lanaguage feature specific to YAML and JSON expressions.
That extend the language to allow for more complex use cases with expressions.
Functions don't apply to script expressions because PowerShell already has rich support for complex manipulation.

!!! Experimental
Functions are a work in progress and subject to change.
_Functions_ are a work in progress and subject to change.
We hope to add more functions, broader support, and more detailed documentation in the future.
[Join or start a disucssion][1] to let us know how we can improve this feature going forward.

[1]: https://github.com/microsoft/PSRule/discussions

Functions cover two (2) main scenarios:

- **Transformation** — you need to perform minor transformation before a condition.
- **Configuration** — you want to configure an input into a condition.

## Using functions

It may be necessary to perform minor transformation before evaluating a condition.
Expand Down
Loading