Skip to content

Function: discriminateFeatureValue()

@kortexya/reasoninglayer


@kortexya/reasoninglayer / 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.

unknown

The raw JSON value to discriminate.

FeatureValueDto

The typed FeatureValueDto value.

If the value cannot be discriminated into a valid FeatureValueDto.

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:

  1. {term_id, var_name} — Variable
  2. {term_id, literal} — Literal (the scalar is discriminated recursively)
  3. {term_id, sort_id, sort_name, features, display} — an inlined Term
  4. {sort_ref, sort_name?} — SortRef
  5. {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.

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
const 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
}