Skip to content

Commit

Permalink
refactor: strengthen RON type validation with strict attribute checking
Browse files Browse the repository at this point in the history
- Add strict attribute name checking for all RON types
- Ensure RON values only contain required attributes
- Replace loose attribute checking with exact key matching
- Standardize validation pattern across all RON types

This change prevents invalid RON values with extra attributes from
being accepted, making the type system more robust.
  • Loading branch information
HeitorAugustoLN committed Dec 21, 2024
1 parent fd3550f commit 9ef961d
Showing 1 changed file with 77 additions and 9 deletions.
86 changes: 77 additions & 9 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@

rawRon = lib.mkOptionType {
check =
value: value ? __type && value.__type == "raw" && value ? value && builtins.isString value.value;
value:
let
keys = builtins.attrNames value;
in
builtins.isAttrs value
&&
[
"__type"
"value"
] == keys
&& value.__type == "raw"
&& builtins.isString value.value;
description = "raw RON value";
descriptionClass = "noun";
merge = lib.options.mergeEqualOption;
Expand All @@ -60,7 +71,18 @@
else
lib.mkOptionType {
check =
value: value ? __type && value.__type == "raw" && value ? value && builtins.elem value.value values;
value:
let
keys = builtins.attrNames value;
in
builtins.isAttrs value
&&
[
"__type"
"value"
] == keys
&& value.__type == "raw"
&& builtins.elem value.value values;
description =
if values == [ ] then
"impossible (empty raw RON enum)"
Expand All @@ -83,9 +105,16 @@
ronChar = lib.mkOptionType {
check =
value:
value ? __type
let
keys = builtins.attrNames value;
in
builtins.isAttrs value
&&
[
"__type"
"value"
] == keys
&& value.__type == "char"
&& value ? value
&& builtins.isString value.value
&& builtins.stringLength value.value == 1;
description = "RON char";
Expand All @@ -96,7 +125,18 @@

ronMap = lib.mkOptionType {
check =
value: value ? __type && value.__type == "map" && value ? value && builtins.isAttrs value.value;
value:
let
keys = builtins.attrNames value;
in
builtins.isAttrs value
&&
[
"__type"
"value"
] == keys
&& value.__type == "map"
&& builtins.isAttrs value.value;
description = "RON map";
descriptionClass = "noun";
merge = lib.options.mergeEqualOption;
Expand All @@ -106,7 +146,17 @@
ronNamedStruct = lib.mkOptionType {
check =
value:
value ? __name && builtins.isString value.__name && value ? value && builtins.isAttrs value.value;
let
keys = builtins.attrNames value;
in
builtins.isAttrs value
&&
[
"__name"
"value"
] == keys
&& builtins.isString value.__name
&& builtins.isAttrs value.value;
description = "RON named struct";
descriptionClass = "noun";
merge = lib.options.mergeEqualOption;
Expand All @@ -116,9 +166,16 @@
ronOptional = lib.mkOptionType {
check =
value:
value ? __type
let
keys = builtins.attrNames value;
in
builtins.isAttrs value
&&
[
"__type"
"value"
] == keys
&& value.__type == "optional"
&& value ? value
&& !builtins.isFunction value.value
&& !builtins.isPath value.value;
description = "RON optional";
Expand All @@ -129,7 +186,18 @@

ronTuple = lib.mkOptionType {
check =
value: value ? __type && value.__type == "tuple" && value ? value && builtins.isList value.value;
value:
let
keys = builtins.attrNames value;
in
builtins.isAttrs value
&&
[
"__type"
"value"
] == keys
&& value.__type == "tuple"
&& builtins.isList value.value;
description = "RON tuple";
descriptionClass = "noun";
merge = lib.options.mergeEqualOption;
Expand Down

0 comments on commit 9ef961d

Please sign in to comment.