Skip to content

Commit

Permalink
👌 [#26] -- addressed remaining PR feedback
Browse files Browse the repository at this point in the history
* Rewrote and renamed the isScalarValue type predicate as arrow func
* Added more TODOs and FIXMEs regarding the component value types
* Made null check strict, as typeof undefined is undefined and not object
* Removed redundant Array.isArray check
  • Loading branch information
sergei-maertens committed Apr 30, 2023
1 parent 063f087 commit aef34e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/lib/validation/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ export const getFormErrors = async (
}
};

function isScalarValue(obj: IValues | Value | Values): obj is Value {
if (Array.isArray(obj)) return false;
if (obj == null) return true;
const isSingleValue = (obj: IValues | Value | Values): obj is Value => {
if (obj === null) return true; // typeof null === 'object'
return typeof obj !== 'object';
}
};

/**
* Validates `form`.
Expand Down Expand Up @@ -114,15 +113,17 @@ export const validateForm = async (
const key = component.key || OF_MISSING_KEY;
// lodash.get like to support nested data structures/keys with dots
// TODO: works only for objects right now
// FIXME: types can be more complex, value of a file upload is not a scaler, but an
// FIXME: types can be more complex, value of a file upload is not a scalar, but an
// object!
// FIXME: the accumulator casting is also less than ideal, it should be possible
// to infer this correctly.
const value = key.split('.').reduce((acc: Value | IValues, bit: string) => {
if (Array.isArray(acc)) {
throw new Error('Arrays not supported yet');
}
if (acc === null) return null;
const nestedProp = (acc as IValues)[bit];
if (isScalarValue(nestedProp)) {
if (isSingleValue(nestedProp)) {
return nestedProp;
}
return nestedProp;
Expand Down
12 changes: 12 additions & 0 deletions src/types/value.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/*
TODO: value types need some overhaul, ideally we make them formio component type
specific through a generic mechanisms. E.g. the value of a file component is
*always* an array of objects, while the value of a checkbox is a boolean (possibly
null?).
Next, the formio component schema allows keys to be specified as foo.bar which
results in a {"foo": {"bar": <value>}} datastructure. Due to their not being a
well-defined, guaranteed mechanism for not-filled out fields, it's possible that
certain (sub) keys are missing and lookups result in 'undefined'.
*/

export type Value = boolean | number | string | null;

export type Values = Value[];
Expand Down

0 comments on commit aef34e4

Please sign in to comment.