Using expressions#
PSRule allows you to write rules using YAML, JSON, or PowerShell. This offers a lot of flexibility to use PSRule for a variety of use cases. Some examples of use cases for each format include:
- YAML — Start authoring quickly with minimal knowledge of PowerShell.
- JSON — Generate rules automatically using automation tools.
- PowerShell — Integrate with other tools using PowerShell cmdlets.
Abstract
This topic covers the differences and limitations between authoring rules using YAML, JSON, and PowerShell. For an example of authoring rules see Writing rules or Testing infrastructure topics.
Language comparison#
Expressions and assertion methods can be used to build similar conditions.
- Expressions — Schema-based conditions written in YAML or JSON. Expressions can be used in rules and selectors.
- Assertion methods — PowerShell-based condition helpers that make rules faster to author. Assertion methods can be used in combination with standard PowerShell code to build rules or conventions.
Quick reference#
In most cases expressions and assertion method names match. There are some cases where these names do not directly align. This lookup table provides a quick reference for expressions and their assertion method counterpart.
Expression | Assertion method |
---|---|
Contains | Contains |
Count | Count |
Equals 1 | n/a |
EndsWith | EndsWith |
Exists | HasField |
Greater | Greater |
GreaterOrEquals | GreaterOrEqual |
HasDefault | HasDefaultValue |
HasSchema | HasJsonSchema |
HasValue 1 | n/a |
In | In |
IsLower | IsLower |
IsString | IsString |
IsUpper | IsUpper |
Less | Less |
LessOrEquals | LessOrEqual |
Match | Match |
NotEquals | n/a |
NotIn | NotIn |
NotMatch | NotMatch |
SetOf | SetOf |
StartsWith | StartsWith |
Subset | Subset |
Version | Version |
n/a | FileHeader |
n/a | FilePath |
n/a | HasFields |
n/a | HasFieldValue 1 |
IsArray | IsArray |
IsBoolean | IsBoolean |
IsDateTime | IsDateTime |
IsInteger | IsInteger |
IsNumeric | IsNumeric |
n/a | JsonSchema |
Exists | NotHasField |
n/a | NotNull |
NotWithinPath | NotWithinPath |
n/a | Null |
n/a | NullOrEmpty |
n/a | TypeOf |
WithinPath | WithinPath |