-
-
Notifications
You must be signed in to change notification settings - Fork 49
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
Optional field save behavior can break validations #241
Comments
In my understanding, |
There are also data format limitations:
|
YAML non-presence would effectively evaluate as cont obj = {};
console.log(typeof obj.field) // undefined I'm not proposing Sveltia write the value For instances, I have an optional object field
If I save a file, even without this object "activated" via the UI switch, I end up with the following in my document, which makes it less readable, and it violates the presumed type contract here, since I have no way of modeling presence / non-presence of the
Basically, the behavior of writing defaults to every field in every case makes it impossible to model a type similar to:
At a minimum I'd say this defult-writing behavior should be optional, since it deviates substantially from Decap CMS. |
Further, Decap / Sveltia supports setting a default. If I wanted an empty string output on an optional field I can set Would you accept a PR to make the implicit-defaults-writing behavior optional? I like and am interested in this project, but this is sort of a non-starter. |
An optional object is supposed to be saved as Omitting an optional field key is not planned. PRs are not accepted at this time. |
Also I want to clarify: Netlify/Decap CMS also saves an empty string on an optional string field in some cases. |
A follow up, for anybody experiencing issues on this front, I was able to get the desired behavior (not writing empty strings for optional fields) with the following custom formatter code. It's working quite well. <script type="importmap">
{
"imports": {
"yaml": "https://esm.sh/[email protected]"
}
}
</script>
<script type="module">
import YAML from 'yaml'
const optionalFields = ['my-field'] // get these somehow...
const formatYAML = (content) => {
for (const [k, c] of Object.entries(content)) {
if (optionalFields.includes(k) && c === '') {
delete content[k];
}
}
return YAML.stringify(content, null, {
lineWidth: 0,
defaultKeyType: 'PLAIN',
defaultStringType: 'PLAIN',
singleQuote: true
}).trim();
}
CMS.registerCustomFormat('yaml-frontmatter', 'md', {
toFile({body, ...content}) {
const delimiter = '---';
body = body ? body + '\\n' : '';
return delimiter + '\\n' + formatYAML(content) + '\\n' + delimiter + '\\n\\n' + body;
}
});
</script> |
I’d introduce a global option for changing the output behaviour. I’m fine as long as it’s consistent (unlike “sometimes omits, sometimes leaves” fuzziness in Netlify/Decap CMS). Stripping optional string type fields is easy — virtually a few-line change — but I need to check other field types. output:
omit_empty_optional_fields: true
yaml:
# YAML specific output options go here
quote: single I know Decap already has a PR for removing deleted image fields, but I’ll cover all the widgets. |
Makes sense, and thanks for considering — I think that would be a nice enhancement. Definitely in favor of eliminating fuzz. In the case of my project, I'm working from I'm unblocked with the method above, and I apologize, you were correct about the |
I see this listed as a feature under Better data output, but the save behavior for fields flagged as
required: false
can lead to data corruption and failed validations. In general it's pretty odd to populate values for data explicitly marked as optional / non-required.An example would be a
string
field that on save is written asmyfield: ''
. If that field had a min length validation in a generator it would now fail to build. Likewise, empty string !=undefined
, and checks or logic built around presence or non-presence of a field would fail to behave as expected.Ideally default values would only be set on fields that explicitly have a
default
value set in the configuration.The text was updated successfully, but these errors were encountered: