-
Hey there! 👋 First off, let me say how much we appreciate this wonderful library - it is really rare to see such a well maintained and documented piece of open-source software out there! 😍 Now, to business: we are currently working on a new product where we are utilizing fireCMS to build the CMS (obviously). Here is a reproducible example: basically, we have objects of type import { buildSchema, buildPropertiesOrBuilder, PropertiesOrBuilder } from '@camberi/firecms';
enum QuizType {
SingleChoice = 'singleChoice',
MultipleChoice = 'multipleChoice',
}
type SingleChoiceContent = {
question: string;
options: string[];
// The index of the correct solution
// In the schema we need to validate that the passed index is valid and not out of bounds
// E.g., if there are 4 answers, the accepted input for the solution should be 0-3
solution: number;
};
type MultipleChoiceContent = {
question: string;
options: string[];
// The indexes of the correct solution; multiple answers can be correct
// In the schema we need to validate that the passed indexes are valid and not out of bounds
// E.g., if there are 4 answers, the accepted input for the solution should be numbers between 0-3; and unique within the array
solution: number[];
};
type Quiz = {
type: QuizType;
content: SingleChoiceContent | MultipleChoiceContent;
};
export const exampleSchema = buildSchema<Quiz>({
properties: {
type: {
title: 'Type',
dataType: 'string',
config: {
enumValues: {
singleChoice: 'single choice',
multipleChoice: 'multiple choice',
},
},
validation: { required: true },
},
content: ({ values }) => {
let properties: PropertiesOrBuilder<any> = {};
switch (values.type) {
// image one case for each type of content; for simplicity, we only define one explicitly
default:
case QuizType.SingleChoice:
properties = buildPropertiesOrBuilder<SingleChoiceContent>({
question: {
title: 'Question',
dataType: 'string',
validation: { required: true },
},
options: {
title: 'Options',
dataType: 'array',
of: {
dataType: 'string',
},
validation: { required: true },
},
solution: ({
values: valuesSolution,
}: {
values: Partial<SingleChoiceContent>;
}): any => {
const { options } = valuesSolution;
const numberOfOptions = options
? Object.values(options)[0].length
: 1;
return {
dataType: 'number',
title: 'Solution',
validation: {
required: true,
positive: true,
integer: true,
min: 0,
max: numberOfOptions - 1,
},
};
},
});
break;
}
return {
dataType: 'map',
title: 'Content',
properties,
};
},
},
}); dependencies: "@camberi/firecms": "^1.0.0-beta9",
"typescript": "^4.4.4"
Any help would be greatly appreciated!! 🙏 Janik |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @jagaSto |
Beta Was this translation helpful? Give feedback.
Hi @jagaSto
You currently can't nest property builders, they can only be used at the top level, and you can build all the child properties from there.
We will consider supporting this in the future