Skip to content

Function: discriminateFeatureValue()

@kortexya/reasoninglayer


@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

FeatureValueDto

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:

  1. TermRef (UUID string)
  2. TermRefs/List (array)
  3. String
  4. Integer (i64)
  5. Real (f64)
  6. Boolean
  7. 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"); // string
const 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
}