Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin-system/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * from './test-utils';
export * from './utils';
export * from './context';
export * from './remote';
export * from './schema';
14 changes: 14 additions & 0 deletions plugin-system/src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export * from './legend-schema';
64 changes: 64 additions & 0 deletions plugin-system/src/schema/legend-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { LegendSchema } from './legend-schema';

describe('legend schema', () => {
test('should pass with minimum required fields', () => {
const validData = {
position: 'bottom',
};
const result = LegendSchema.safeParse(validData);
expect(result.success).toBe(true);
console.log(result.error);
});

test('should pass with all fields provided', () => {
const validData = {
position: 'bottom',
mode: 'table',
size: 'medium',
values: ['sum', 'abs'],
};
const result = LegendSchema.safeParse(validData);
expect(result.success).toBe(true);
});

test('should fail if position is missing', () => {
const invalidData = {
mode: 'list',
};
const result = LegendSchema.safeParse(invalidData);
expect(result.success).toBe(false);
if (!result.success) {
expect(result?.error?.issues?.[0]?.path).toContain('position');
}
});

test('should fail if an invalid position value is provided', () => {
const invalidData = {
position: 'center',
};
const result = LegendSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});

test('should handle empty values array', () => {
const validData = {
position: 'bottom',
values: [],
};
const result = LegendSchema.safeParse(validData);
expect(result.success).toBe(true);
});
});
25 changes: 25 additions & 0 deletions plugin-system/src/schema/legend-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import z from 'zod';
import { legendModes, legendPositions, legendSizes } from '@perses-dev/core';
import { legendValues } from '../model/legend';

const allValueOptions = [...legendValues, 'abs', 'relative'] as const;

export const LegendSchema = z.object({
position: z.enum([...legendPositions]),
mode: z.enum([...legendModes]).optional(),
size: z.enum([...legendSizes]).optional(),
values: z.array(z.enum(allValueOptions as unknown as [string, ...string[]])).optional(),
});
Loading