You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Palantir Workshop表单:TypeScript实现(userId,startdate,states)组合验证

Palantir Ontology表单输入组合重复验证实现方案

需求背景

在Palantir Workshop的表单提交流程中,需要验证userId、startdate、states的组合是否已存在,避免重复创建Ontology对象。以下是基于你提供的代码改造的完整实现方案。

现有代码(已翻译修正)

import {
    OntologyEditFunction,
    LocalDate,
    Integer, 
    Users,
    Double,
    FunctionsMap, 
    Function,
} from "@foundry/functions-api";
import {
    Objects, 
    ObjectSet,
    ObjectWeUse,
} from "@foundry/ontology-api";

@OntologyEditFunction()
public async createSkus(
  userId: string,
  is_Sku_Pending?: string,
  partition?: string,
  startdate?: LocalDate,
  all_states?: string,
  states?: string[], // 支持多选值
): Promise<void> {
        let final_states;
        
        if (all_states && all_states === "All") {
            final_states = U.all_usa_states; // 预定义常量字符串数组
        } else if (all_states && all_states === "All but UT") {
            final_states = U.all_usa_states_without_utah; // 预定义常量字符串数组
        } else {
            final_states = states;
        }

        // 原代码中packages变量未定义,此处修正为循环处理final_states
        for (let i = 0; i < (final_states?.length || 0); i++) {
            let sku= Objects.create().ObjectWeUse(U.uuidv4());
            sku.userId= userId; // 修正原代码错误:应使用传入的userId而非新生成的UUID
            sku.states= final_states;
            sku.strs = startdate;
            sku.partition = partition;
            sku.isSkuPending = is_Sku_Pending;
        }
}

验证逻辑实现步骤

1. 添加组合存在性查询

在创建对象前,通过Ontology查询API检索是否已有匹配的ObjectWeUse对象:

// 放在final_states确定之后、循环创建对象之前
if (!userId || !startdate || !final_states) {
    throw new Error("userId、startdate和states为必填项");
}

// 构建查询条件:匹配userId、startdate、states数组
const existingSkus: ObjectSet<ObjectWeUse> = Objects.search().ObjectWeUse()
    .filter(sku => sku.userId.exactMatch(userId))
    .filter(sku => sku.strs.exactMatch(startdate))
    .filter(sku => sku.states.arrayEquals(final_states));

// 检查是否存在匹配结果
const exists = await existingSkus.count() > 0;

if (exists) {
    throw new Error(`组合(userId: ${userId},日期: ${startdate.toString()},州: ${final_states.join(',')})已存在,无法重复创建`);
}

2. 完整代码示例

import {
    OntologyEditFunction,
    LocalDate,
    Integer, 
    Users,
    Double,
    FunctionsMap, 
    Function,
} from "@foundry/functions-api";
import {
    Objects, 
    ObjectSet,
    ObjectWeUse,
} from "@foundry/ontology-api";

@OntologyEditFunction()
public async createSkus(
  userId: string,
  is_Sku_Pending?: string,
  partition?: string,
  startdate?: LocalDate,
  all_states?: string,
  states?: string[], // 支持多选值
): Promise<void> {
        let final_states;
        
        if (all_states && all_states === "All") {
            final_states = U.all_usa_states; // 预定义常量字符串数组
        } else if (all_states && all_states === "All but UT") {
            final_states = U.all_usa_states_without_utah; // 预定义常量字符串数组
        } else {
            final_states = states;
        }

        // 基础必填校验
        if (!userId || !startdate || !final_states) {
            throw new Error("userId、startdate和states为必填项");
        }

        // 验证组合是否已存在
        const existingSkus: ObjectSet<ObjectWeUse> = Objects.search().ObjectWeUse()
            .filter(sku => sku.userId.exactMatch(userId))
            .filter(sku => sku.strs.exactMatch(startdate))
            .filter(sku => sku.states.arrayEquals(final_states));

        const exists = await existingSkus.count() > 0;

        if (exists) {
            throw new Error(`组合(userId: ${userId},日期: ${startdate.toString()},州: ${final_states.join(',')})已存在,无法重复创建`);
        }

        // 循环创建对象
        for (let i = 0; i < final_states.length; i++) {
            let sku= Objects.create().ObjectWeUse(U.uuidv4());
            sku.userId= userId;
            sku.states= final_states;
            sku.strs = startdate;
            sku.partition = partition;
            sku.isSkuPending = is_Sku_Pending;
        }
}

关键细节说明

  • 数组匹配规则:使用arrayEquals确保states数组的元素和顺序完全一致;若只需元素相同不考虑顺序,可替换为sku.states.arrayContainsAll(final_states)且sku.states.arrayIsContainedIn(final_states)的组合判断。
  • 错误处理:抛出的错误会被前端捕获,可用于提示用户重复提交;也可根据需求改为返回布尔值或自定义响应格式。
  • 参数修正:原代码中sku.userId错误赋值为新UUID,已修正为使用传入的userId参数,确保验证逻辑有效。

内容的提问来源于stack exchange,提问作者Shiva Golla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 13:40:10