0% found this document useful (0 votes)
12 views3 pages

Documentation Service

This document defines a DocumentationService class that generates a Joi schema from a properties list defined in a pufSchema. The constructor takes the pufSchema and stores the properties list. The generateModelJoiSchema method iterates through the properties list, determines the field schema based on the property type, and builds up an object of field schemas to return as a Joi object schema.

Uploaded by

Lucas Bo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Documentation Service

This document defines a DocumentationService class that generates a Joi schema from a properties list defined in a pufSchema. The constructor takes the pufSchema and stores the properties list. The generateModelJoiSchema method iterates through the properties list, determines the field schema based on the property type, and builds up an object of field schemas to return as a Joi object schema.

Uploaded by

Lucas Bo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

const Joi = require('joi');

const moment = require('moment');

class DocumentationService {
constructor(pufSchema) {
this.pufSchema = pufSchema;
this.propertiesList = pufSchema.propertiesList;
}

get recordShape() {
return Joi.object();
}

generateModelJoiSchema() {
const propertiesListSchemaObject = {};

for (const property of this.propertiesList) {


let fieldSchema = null;

switch (property.type) {
case 'string':
fieldSchema = Joi.string().allow('', null).example('Lorem Ipsum');
break;
case 'slug':
fieldSchema = Joi.string().allow('', null).example('lorem-ipsum');
break;
case 'richtext':
fieldSchema = Joi.object({
editorStateJson: Joi.string()
.allow('', null)
.example(
'{\n "blocks": [\n {\n "key": "7i54t",\n "text":
"Lorem Ipsum",\n "type": "unstyled",\n "depth": 0,\n
"inlineStyleRanges": [],\n "entityRanges": [],\n "data": {}\n }\n ],\
n "entityMap": {}\n}'
),
html: Joi.string().allow('', null).example('<p>Lorem Ipsum</p>'),
})
.allow(null)
.label('FieldType-richtext');
break;
case 'enum': {
let possibleValuesSchema;

if (property.values) {
possibleValuesSchema = Joi.string()
.allow('', null)
.valid(...property.values.map((enumValue) => enumValue.value));
} else {
// Handle legacy dataSource
possibleValuesSchema = Joi.any();
}

if (property.multiple) {
fieldSchema = Joi.array().items(possibleValuesSchema);
} else {
fieldSchema = possibleValuesSchema;
}
break;
}
case 'int':
case 'float':
fieldSchema = Joi.number().allow(null);
break;
case 'bool':
fieldSchema = Joi.boolean().allow(null);
break;
case 'timestamp':
fieldSchema = Joi.date()
.timestamp()
.allow(null)
.example(moment().unix());
break;
case 'list':
fieldSchema = Joi.array().allow(null);
break;
case 'object':
fieldSchema = Joi.object().allow(null).label('FieldType-object');
break;
case 'object_ref':
fieldSchema = Joi.object()
.allow(null)
.label('FieldType-object_ref')
.example({ _id: 'id_123', _cls: 'Resource' });
break;
case 'file':
fieldSchema = Joi.string().allow('', null).description('Fichier');
break;
case 'object_id':
fieldSchema = Joi.string().allow('', null).example('id_123');
if (Array.isArray(property.objectType)) {
fieldSchema = fieldSchema.description(
`Référence id vers le(s) ressource(s) de type $
{property.objectType.map(
(type) => type
)}`
);
}
break;
case 'layout':
fieldSchema = Joi.array().items(Joi.any());
break;
case 'mongooseRef':
fieldSchema = Joi.string().allow('', null);
if (Array.isArray(property.objectType)) {
fieldSchema = fieldSchema.description(
`Référence id vers le(s) ressource(s) de type $
{property.objectType.map(
(type) => type
)}`
);
}
break;
case 'color':
fieldSchema = Joi.string().example('#000000').allow('', null);
break;
case 'image':
fieldSchema = Joi.object({
// _id: Joi.string(), // mongoDB objectId
file: Joi.string().allow('', null).example('/2023/04/01/bird.png'),
label: Joi.string().allow('', null).example('Bird'),
caption: Joi.string()
.allow('', null)
.example('Image of a yellow bird'),
})
.allow(null)
.label('FieldType-image');
break;
case 'FrontIcon':
fieldSchema = Joi.object().allow(null).label('FieldType-FrontIcon');
break;
case 'FrontColor':
fieldSchema = Joi.string().allow('', null);
break;
case 'Link':
fieldSchema = Joi.object().allow(null).label('FieldType-Link');
break;
default:
fieldSchema = Joi.any();
}

// Don't put required unless we are confident all our data strictly adhere to
our schema definition
// if (property.required) {
// fieldSchema = fieldSchema.required();
// }

if (property.description) {
fieldSchema = fieldSchema.description(property.description);
}

propertiesListSchemaObject[property.key] = fieldSchema;
}

return Joi.object(propertiesListSchemaObject).label(this.pufSchema._id);
}
}

module.exports = DocumentationService;

You might also like