1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-05 17:53:12 +02:00

Feat: update schemas to use 'variants' in place of 'variant'

This commit is contained in:
Thomas Heartman 2022-07-19 12:02:09 +02:00
parent 789b70dcfe
commit f4ed7d0893
5 changed files with 61 additions and 61 deletions

View File

@ -1,4 +1,5 @@
import fc, { Arbitrary } from 'fast-check'; import fc, { Arbitrary } from 'fast-check';
import { WeightType } from '../../../lib/types/model';
import { urlFriendlyString } from '../../../test/arbitraries.test'; import { urlFriendlyString } from '../../../test/arbitraries.test';
import { validateSchema } from '../validate'; import { validateSchema } from '../validate';
import { import {
@ -12,28 +13,42 @@ export const generate = (): Arbitrary<PlaygroundFeatureSchema> =>
isEnabled: fc.constant(isEnabled), isEnabled: fc.constant(isEnabled),
projectId: urlFriendlyString(), projectId: urlFriendlyString(),
name: urlFriendlyString(), name: urlFriendlyString(),
variant: fc.record( variants: fc.uniqueArray(
{ fc.record(
name: urlFriendlyString(), {
enabled: fc.constant(isEnabled), name: urlFriendlyString(),
payload: fc.oneof( enabled: fc.constant(isEnabled),
fc.record({ payload: fc.oneof(
type: fc.constant('json' as 'json'), fc.record({
value: fc.json(), type: fc.constant('json' as 'json'),
}), value: fc.json(),
fc.record({ }),
type: fc.constant('csv' as 'csv'), fc.record({
value: fc type: fc.constant('csv' as 'csv'),
.array(fc.lorem()) value: fc
.map((words) => words.join(',')), .array(fc.lorem())
}), .map((words) => words.join(',')),
fc.record({ }),
type: fc.constant('string' as 'string'), fc.record({
value: fc.string(), type: fc.constant('string' as 'string'),
}), value: fc.string(),
), }),
}, ),
{ requiredKeys: ['name', 'enabled'] }, weight: fc.nat({ max: 1000 }),
weightType: fc.constant(WeightType.VARIABLE),
stickiness: fc.constant('default'),
},
{
requiredKeys: [
'name',
'enabled',
'weight',
'stickiness',
'weightType',
],
},
),
{ selector: (variant) => variant.name },
), ),
}), }),
); );

View File

@ -1,4 +1,6 @@
import { FromSchema } from 'json-schema-to-ts'; import { FromSchema } from 'json-schema-to-ts';
import { overrideSchema } from './override-schema';
import { variantSchema } from './variant-schema';
export const playgroundFeatureSchema = { export const playgroundFeatureSchema = {
$id: '#/components/schemas/playgroundFeatureSchema', $id: '#/components/schemas/playgroundFeatureSchema',
@ -6,36 +8,28 @@ export const playgroundFeatureSchema = {
'A simplified feature toggle model intended for the Unleash playground.', 'A simplified feature toggle model intended for the Unleash playground.',
type: 'object', type: 'object',
additionalProperties: false, additionalProperties: false,
required: ['name', 'projectId', 'isEnabled', 'variant'], required: ['name', 'projectId', 'isEnabled', 'variants'],
properties: { properties: {
name: { type: 'string', examples: ['my-feature'] }, name: { type: 'string', examples: ['my-feature'] },
projectId: { type: 'string', examples: ['my-project'] }, projectId: { type: 'string', examples: ['my-project'] },
isEnabled: { type: 'boolean', examples: [true] }, isEnabled: { type: 'boolean', examples: [true] },
variant: { variants: {
type: 'object', type: 'array',
additionalProperties: false, items: {
required: ['name', 'enabled'], allOf: [
properties: { { $ref: variantSchema.$id },
name: { type: 'string' }, {
enabled: { type: 'boolean' }, type: 'object',
payload: { required: ['enabled'],
type: 'object', properties: {
additionalProperties: false, enabled: { type: 'boolean', examples: [true] },
required: ['type', 'value'],
properties: {
type: {
type: 'string',
enum: ['json', 'csv', 'string'],
}, },
value: { type: 'string' },
}, },
}, ],
}, },
nullable: true,
examples: ['green'],
}, },
}, },
components: { schemas: {} }, components: { schemas: { variantSchema, overrideSchema } },
} as const; } as const;
export type PlaygroundFeatureSchema = FromSchema< export type PlaygroundFeatureSchema = FromSchema<

View File

@ -5,12 +5,14 @@ import {
} from '../../../lib/openapi/spec/playground-response-schema'; } from '../../../lib/openapi/spec/playground-response-schema';
import { validateSchema } from '../validate'; import { validateSchema } from '../validate';
import { generate as generateInput } from './playground-request-schema.test'; import { generate as generateInput } from './playground-request-schema.test';
import { generate as generateToggles } from './playground-feature-schema.test'; import { generate as generateFeature } from './playground-feature-schema.test';
const generate = (): Arbitrary<PlaygroundResponseSchema> => const generate = (): Arbitrary<PlaygroundResponseSchema> =>
fc.record({ fc.record({
input: generateInput(), input: generateInput(),
features: fc.array(generateToggles()), features: fc.uniqueArray(generateFeature(), {
selector: (feature) => feature.name,
}),
}); });
test('playgroundResponseSchema', () => test('playgroundResponseSchema', () =>

View File

@ -3,6 +3,8 @@ import { sdkContextSchema } from './sdk-context-schema';
import { playgroundRequestSchema } from './playground-request-schema'; import { playgroundRequestSchema } from './playground-request-schema';
import { playgroundFeatureSchema } from './playground-feature-schema'; import { playgroundFeatureSchema } from './playground-feature-schema';
import { featureVariantsSchema } from './feature-variants-schema'; import { featureVariantsSchema } from './feature-variants-schema';
import { variantSchema } from './variant-schema';
import { overrideSchema } from './override-schema';
export const playgroundResponseSchema = { export const playgroundResponseSchema = {
$id: '#/components/schemas/playgroundResponseSchema', $id: '#/components/schemas/playgroundResponseSchema',
@ -17,21 +19,7 @@ export const playgroundResponseSchema = {
features: { features: {
type: 'array', type: 'array',
items: { items: {
allOf: [ $ref: playgroundFeatureSchema.$id,
{ $ref: playgroundFeatureSchema.$id },
{
type: 'object',
required: ['variants'],
properties: {
variants: {
type: 'array',
items: {
$ref: featureVariantsSchema.$id,
},
},
},
},
],
}, },
}, },
}, },
@ -41,6 +29,8 @@ export const playgroundResponseSchema = {
playgroundFeatureSchema, playgroundFeatureSchema,
playgroundRequestSchema, playgroundRequestSchema,
sdkContextSchema, sdkContextSchema,
variantSchema,
overrideSchema,
}, },
}, },
} as const; } as const;

View File

@ -4,7 +4,6 @@ import { overrideSchema } from './override-schema';
export const variantSchema = { export const variantSchema = {
$id: '#/components/schemas/variantSchema', $id: '#/components/schemas/variantSchema',
type: 'object', type: 'object',
additionalProperties: false,
required: ['name', 'weight', 'weightType', 'stickiness'], required: ['name', 'weight', 'weightType', 'stickiness'],
properties: { properties: {
name: { name: {