Function: discriminateFeatureValue()
@kortexya/reasoninglayer / discriminateFeatureValue
Function: discriminateFeatureValue()
discriminateFeatureValue(
value):FeatureValueDto
Defined in: src/utils/discrimination.ts:68
Discriminate an unknown JSON value into a typed FeatureValueDto.
Parses raw JSON values from homoiconic inference responses into the
FeatureValueDto type. This is needed because the untagged format
uses raw JSON primitives with no type discriminator.
Parameters
value
unknown
The raw JSON value to discriminate.
Returns
The typed FeatureValueDto value.
Throws
If the value cannot be discriminated into a valid FeatureValueDto.
Remarks
Ambiguity note: UUID strings and regular strings are both string in TypeScript.
This function does NOT distinguish between them — both return as string.
Use isUuid separately if you need to determine whether a string value
is a term reference (UUID) or a plain string.
The backend’s Rust #[serde(untagged)] deserialization order is:
- TermRef (UUID string)
- TermRefs/List (array)
- String
- Integer (i64)
- Real (f64)
- Boolean
- Null (uninstantiated)
Since JavaScript number does not distinguish i64 from f64, this function
uses Number.isInteger() as a heuristic. The caller can use this to decide
whether to treat a number as integer or real.
Example
import { discriminateFeatureValue, isUuid } from '@kortexya/reasoninglayer';
const val = discriminateFeatureValue("hello"); // stringconst num = discriminateFeatureValue(42); // number (integer)const arr = discriminateFeatureValue(["a", "b"]); // string[]const nil = discriminateFeatureValue(null); // null
// To check if a string is a term reference:if (typeof val === "string" && isUuid(val)) { // It's a term reference UUID}