rule.ts•3.14 kB
/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/
import * as z from "zod";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
Clause,
Clause$inboundSchema,
Clause$Outbound,
Clause$outboundSchema,
} from "./clause.js";
import {
Rollout,
Rollout$inboundSchema,
Rollout$Outbound,
Rollout$outboundSchema,
} from "./rollout.js";
export type Rule = {
/**
* The flag rule ID
*/
id?: string | undefined;
/**
* The index of the variation, from the array of variations for this flag
*/
variation?: number | undefined;
rollout?: Rollout | undefined;
/**
* An array of clauses used for individual targeting based on attributes
*/
clauses: Array<Clause>;
/**
* Whether LaunchDarkly tracks events for this rule
*/
trackEvents: boolean;
/**
* The rule description
*/
description?: string | undefined;
ref?: string | undefined;
};
/** @internal */
export const Rule$inboundSchema: z.ZodType<Rule, z.ZodTypeDef, unknown> = z
.object({
_id: z.string().optional(),
variation: z.number().int().optional(),
rollout: Rollout$inboundSchema.optional(),
clauses: z.array(Clause$inboundSchema),
trackEvents: z.boolean(),
description: z.string().optional(),
ref: z.string().optional(),
}).transform((v) => {
return remap$(v, {
"_id": "id",
});
});
/** @internal */
export type Rule$Outbound = {
_id?: string | undefined;
variation?: number | undefined;
rollout?: Rollout$Outbound | undefined;
clauses: Array<Clause$Outbound>;
trackEvents: boolean;
description?: string | undefined;
ref?: string | undefined;
};
/** @internal */
export const Rule$outboundSchema: z.ZodType<Rule$Outbound, z.ZodTypeDef, Rule> =
z.object({
id: z.string().optional(),
variation: z.number().int().optional(),
rollout: Rollout$outboundSchema.optional(),
clauses: z.array(Clause$outboundSchema),
trackEvents: z.boolean(),
description: z.string().optional(),
ref: z.string().optional(),
}).transform((v) => {
return remap$(v, {
id: "_id",
});
});
/**
* @internal
* @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
*/
export namespace Rule$ {
/** @deprecated use `Rule$inboundSchema` instead. */
export const inboundSchema = Rule$inboundSchema;
/** @deprecated use `Rule$outboundSchema` instead. */
export const outboundSchema = Rule$outboundSchema;
/** @deprecated use `Rule$Outbound` instead. */
export type Outbound = Rule$Outbound;
}
export function ruleToJSON(rule: Rule): string {
return JSON.stringify(Rule$outboundSchema.parse(rule));
}
export function ruleFromJSON(
jsonString: string,
): SafeParseResult<Rule, SDKValidationError> {
return safeParse(
jsonString,
(x) => Rule$inboundSchema.parse(JSON.parse(x)),
`Failed to parse 'Rule' from JSON`,
);
}