YAML object properties - yaml

I am defining code review configuration in YAML. I need to keep the config as compact as possible without explicitly defining xml/json like name value pairs.
- group: Naming convention
severity: medium
rules:
- name: Check API naming convention
type: pattern
element: api.#name
pattern: '.*\-.*\-\d\.\d'
properties:
- exit-on-fail
- skip-and-proceed
- etc.
What I don't like here is defining the tag "properties" to add the actions. Can the actions exist at the object level?

Indentation-wise, they can exist on the object level:
- group: Naming convention
severity: medium
rules:
- name: Check API naming convention
type: pattern
element: api.#name
pattern: '.*\-.*\-\d\.\d'
properties:
- exit-on-fail
- skip-and-proceed
- etc.
This works because YAML sees - as indentation and therefore, this still creates a list as value for the properties: key.
To compactify, you can also write them inline like daggett suggested:
- group: Naming convention
severity: medium
rules:
- name: Check API naming convention
type: pattern
element: api.#name
pattern: '.*\-.*\-\d\.\d'
properties: [exit-on-fail, skip-and-proceed, etc]
Finally, you can put them into your object mapping as long as they do not share a name with any other fields:
- group: Naming convention
severity: medium
rules:
- name: Check API naming convention
type: pattern
element: api.#name
pattern: '.*\-.*\-\d\.\d'
? exit-on-fail
? skip-and-proceed
? etc.
This creates three additional key-value pairs in your object, with the three properties being the keys and the empty string (possibly a null value depending on the YAML implementation you use) being the value. If you do this, you will need to write a custom constructor to load this into a native data structure, because you need to differentiate between the object fields and the actions. How to do this, again, depends on your YAML implementation.

Related

How to add item to YML collection conditionally using Serverless framework?

Given I have the following defined in a YML file for a resource:
Type: AWS::Cognito::UserPoolClient
CallbackURLs:
- "my-first-url"
How can I add a second item when only certain conditions are met? Unfortuantely it is not possible to simply set it to an empty string or null as it fails deployment validation. E.G something like:
Type: AWS::Cognito::UserPoolClient
CallbackURLs:
- "my-first-url"
- myCondition ? "" : undefined // Omit item
Is this possible in any way at all? Happy to use Plugin solutions etc.
You can use a CloudFormation condition like Fn::If to conditionally create stack resources. The CloudFormation documentation about conditions has all the details but somehting like this should get you started:
resources:
Conditions:
my_condition: !Equals [value_1, value_2]
Resources:
MyUserPool:
Type: AWS::Cognito::UserPoolClient
CallbackURLs:
- "my-first-url"
- !If [my_condition, "...", !Ref "AWS::NoValue"]
Replace the content of my_condition with your condition. It is referenced later in the Fn::If (the example uses the shorthand for Fn::If).
The AWS::NoValue is a pseudo parameter which can be used as a return value to remove the corresponding property. It should work here to remove the list item but I'm not sure about it, you'll need to test.

Issue with creating a documentation when using re-usable enums

The issue with creating documentation when using re-usable enums
My yaml file looks like this
openapi: 3.0.2 components: schemas:
Countries:
type: string
enum:
- Unknown
- Afghanistan
- Albania
- Algeria
- American Samoa
- "\u00c5landIslands"
- NotOtherwiseSpecified
, when I compile it creates the java classes correctly, however, is not creating the documentation, just gives me:
Countries-
and nothing more is displayed for that particular scheme. For the other schemes enum options are displayed, etc.
Can you help me with this problem? Is this an issue of swagger or I am mistaken somewhere. The example in the swagger website and my code are following the same rules: https://swagger.io/docs/specification/data-models/enums/.
I also posted similar question here: https://community.smartbear.com/t5/SwaggerHub/Issue-with-creating-a-documentation-when-using-re-usable-enums/m-p/191938
P.S. I thought the problem is coming because of the special character, but it is not. I tried without that specific enum entry and also I have another similar way reusable enum that behaves the same way.
This is working fine for me. I am attaching an example for reference of how I am doing it.
Parameters in route.yaml
parameters:
- name: Country
in: query
required: true
schema:
$ref: "#/components/schemas/test"
Declaration in parameters.yaml
components:
schemas:
test:
type: string
enum:
- Unknown
- Afghanistan
- Albania
- Algeria
- "\u00c5landIslands"
Attaching the image from Swagger UI
Please add some more details for reference as above mentioned are working fine for me.

How to change one value in an associative array referenced by Node

So I apologize in advance if I use yaml terminology wrong I am pretty new to it.
So I have this item in a list that is an associative array and I would like to use a node to repeat it multiple times in the file but I need to change one value in a sub array in it and I don't know how to do it without overwriting the entire array.
So here is the item in the list
- &def_service
type: service
name: Remote Service
config:
machine: ''
version: '1.0.0'
apikey: VALUE_I_WANT_TO_CHANGE
and I what I've tried to do is
- <<: *def_service
config:
apikey: NEW_VALUE
but that just overwrites the entire array so config is just
{config:{apikey:NEW_VALUE}}
I would be very grateful for an answer here I am pretty stuck.
Ok so an answer that occurred to me maybe not the best answer is just to introduce another variable for the config array like this.
- &def_service
type: service
name: Remote Service
config: &service_config
machine: ''
version: '1.0.0'
apikey: VALUE_I_WANT_TO_CHANGE
so to reference it I did this
- <<: *def_service
config:
<<: *service_config
apikey: NEW_VALUE
YAML is not a programming language. It is designed for data representation, not for data transformation.
The merge key you use (<<) is not part of the YAML specification. It is part of the YAML type repository, which is outdated (since it is defined for YAML 1.1). Therefore, your question is highly dependendent on the YAML processor you use. One processor might implement it while another does not.
Since you have a specific problem, it would probably be better to write YAML tailored to your problem, and then handle it in your code (assuming that you are in charge of the code). Something like this:
- !config_base
&def_service
type: service
name: Remote Service
config: &service_config
machine: ''
version: '1.0.0'
apikey: VALUE_I_WANT_TO_CHANGE
- !config_child
base: *def_service
substitutions:
config:
apikey: NEW_VALUE
You can then write code that does the substitution inside your deserialized YAML structure.

YAML Local Anchor?

I'm trying to simplify this data:
farm:
- pets:
- type: fish
breedtype: fish
name: dory
- type: dog
breedtype: dog
name: lassie
Basically type and breedtype are always the same, unfortunately this is the case.
I've tried to simplify this by somehow using anchors
base: &base
type: &type fish
breedtype: *type
farm:
type: &type dog
<<: *base
I want the *base to inherit &type from &type dog.
I'm not sure if this is the right approach or if there's an easier way, am I looking at the problem wrong?
While it is true that YAML defines a << type, be aware that this is not part of the specification, but part of the type repository which is based on the outdated YAML version 1.1 and has not been updated since. A YAML implementation is not required to support this type. Moreover, it does not do what you think it does, because the alias *type is resolved in the context where it occurs and will always link to the value fish.
Be aware that YAML is pure data. It is not capable of nor designed to enforce rules like type always must match breedtype. The schema used for loading the data is defined by the one loading it. If in your data schema, type always equals breedtype, you can just leave out breedtype in YAML, because when loading it into an application, you can set its value from type.

Kwalify YAML Validation - Use regexes in key names?

I'm using Kwalify for schema validation. One part of the YAML document actually does want to allow for key names of a certain type in a mapping.
I see that Kwalify support regexes for the values in a mapping, but I see no such mention of support for using regexes in the keys in a mapping. Here's what I'd like to support validating:
test-element:
sub-element-1: test
sub-element-2:
element-with-pattern-1: test1
element-with-pattern-2: test2
So I don't know what some key names will exactly be in advance (shown here with the fake names "element-with-pattern-*", but I do know that they should correspond to a pattern defined by a regex.
Is this possible to validate using Kwalify?
To check this:
parent_key:
random_key1: url1
random_key2: url2
you should use "Default of Mapping", here is schema example:
type: map
mapping:
"parent_key":
type: map
mapping:
"=":
type: str
http://www.kuwata-lab.com/kwalify/ruby/users-guide.02.html#tips-default
I do not believe it's possible given the current state of the code.
I'm actually in a similar situation, which I found out (the hard way) does not work well for validation in the Kwalify context. I've begun migrating away from flexible key names into a paradigm where I can specifically define the schema.
For example, I migrated this:
parent_key:
random_key1: url1
random_key2: url2
To:
parent_key:
- name: random_key1
url: url1
- name: random_key2
url: url2
With the latter syntax, you can validate like this:
"parent_key":
type: seq
sequence:
- type: map
mapping:
"name":
type: str
required: yes
"url":
type: str
required: yes
Within that context, you can add a pattern regex validators to either name or url that should allow you to achieve your goal.

Resources