YAML Quotes Guidelines
tip
This page is current and ready for use.
In this project, we aim to use YAML configurations consistently and efficiently. Based on experience and observations (including inconsistencies in the official Overblog documentation), we recommend the following:
General Rule
- Do not use quotes (single or double) unless necessary.
When Quotes Are Not Needed
-
Simple Field Values:
- Plain strings, integers, booleans, or lists without special characters.
- Examples:
id:
type: ID!
name:
type: String
enabled: true
limit: 10
roles:
- admin
- user
- guest
-
GraphQL Type Definitions:
- Common GraphQL types such as
String
,Int
,Boolean
, or non-null types (ID!
). - Examples:
User:
type: object
config:
fields:
id:
type: ID!
email:
type: String!
- Common GraphQL types such as
When Quotes Are Required
-
Special Characters:
- If a value includes symbols like
@
,#
,:
,{}
, or other special characters. - Example:
resolve: '@=query("App\\User\\GraphQL\\Resolver\\UserResolver::getUsers", args)'
- If a value includes symbols like
-
Strings Starting with Reserved Characters:
- Strings starting with
*
,&
,#
, or:
to prevent YAML misinterpretation. - Example:
default_value: "*any"
- Strings starting with
-
Whitespace or Complex Text:
- For strings with spaces, newlines, or unusual formatting.
- Example:
notes: "User's full name and contact details."
-
Avoiding YAML’s Implicit Typing:
- Words like
yes
,no
,on
,off
, etc., should be quoted to avoid being interpreted as booleans. - Example:
status: "no"
- Words like
-
Environment Variables with Special Characters:
- Use quotes when values include
%
,$
, parentheses, or other special characters. - Always use double quotes for environment variables for consistency.
- Example:
api_key: "%env(API_KEY)%"
- Use quotes when values include
-
Array Type in GraphQL:
- Always quote array types to ensure proper parsing.
- Example:
items:
type: "[String]"
-
Description:
- Always quote description to ensure proper parsing.
- Example:
items:
type: "[String]"
description: "This is a collection of multiple items"
Recommended Style
- Avoid quotes for simple cases to keep the YAML clean and readable.
- Use quotes when dealing with special cases as outlined above.
- Maintain consistency throughout the codebase.
Examples
General Use Case (Preferred for Simplicity)
User:
type: object
config:
description: "A user object"
fields:
id: ID!
email: String
isActive: Boolean
gender: "[String]"
By following these guidelines, developers can ensure clean, consistent, and error-free YAML configurations across the project.
For a detailed information, you can refer here.