Function: discriminateFeatureValue()
@kortexya/reasoninglayer / discriminateFeatureValue
Function: discriminateFeatureValue()
Section titled “Function: discriminateFeatureValue()”discriminateFeatureValue(
value):FeatureValueDto
Defined in: src/utils/discrimination.ts:104
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
Section titled “Parameters”unknown
The raw JSON value to discriminate.
Returns
Section titled “Returns”The typed FeatureValueDto value.
Throws
Section titled “Throws”If the value cannot be discriminated into a valid FeatureValueDto.
Remarks
Section titled “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.
Objects are narrowed by their keys, in the engine’s #[serde(untagged)]
variant order:
{term_id, var_name}— Variable{term_id, literal}— Literal (the scalar is discriminated recursively){term_id, sort_id, sort_name, features, display}— an inlined Term{sort_ref, sort_name?}— SortRef{term_id}— TermRef
then arrays (each element recursively), strings, numbers, booleans and
null. An object that matches none of the five shapes throws.
Since JavaScript number does not distinguish i64 from f64, this function
returns every number as number. Use Number.isInteger() if the
distinction matters.
Example
Section titled “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); // nullconst sort = discriminateFeatureValue({ sort_ref: "…", sort_name: "k_kind" }); // SortRefFeatureValue
// To check if a string is a term reference:if (typeof val === "string" && isUuid(val)) { // It's a term reference UUID}