cleaned plugin validate from cli

This commit is contained in:
NEOLPAR 2022-09-09 11:25:17 +01:00
parent 8261549131
commit 829d1bf70d
4 changed files with 5 additions and 100 deletions

View File

@ -1,6 +0,0 @@
exports.PluginTypes = {
COMPONENT: "component",
DATASOURCE: "datasource",
}
exports.PLUGIN_TYPES_ARR = Object.values(exports.PluginTypes)

View File

@ -3,8 +3,8 @@ const { CommandWords } = require("../constants")
const { getSkeleton, fleshOutSkeleton } = require("./skeleton")
const questions = require("../questions")
const fs = require("fs")
const { PLUGIN_TYPES_ARR } = require("./constants")
const { validate } = require("./validate")
const { PLUGIN_TYPE_ARR } = require("@budibase/types")
const { validate } = require("@budibase/backend-core/plugins")
const { runPkgCommand } = require("../exec")
const { join } = require("path")
const { success, error, info } = require("../utils")
@ -24,7 +24,7 @@ function checkInPlugin() {
async function init(opts) {
const type = opts["init"] || opts
if (!type || !PLUGIN_TYPES_ARR.includes(type)) {
if (!type || !PLUGIN_TYPE_ARR.includes(type)) {
console.log(
error(
"Please provide a type to init, either 'component' or 'datasource'."

View File

@ -1,91 +0,0 @@
const { PluginTypes } = require("./constants")
const { DatasourceFieldType, QueryType } = require("@budibase/types")
const joi = require("joi")
const DATASOURCE_TYPES = [
"Relational",
"Non-relational",
"Spreadsheet",
"Object store",
"Graph",
"API",
]
function runJoi(validator, schema) {
const { error } = validator.validate(schema)
if (error) {
throw error
}
}
function validateComponent(schema) {
const validator = joi.object({
type: joi.string().allow("component").required(),
metadata: joi.object().unknown(true).required(),
hash: joi.string().optional(),
version: joi.string().optional(),
schema: joi
.object({
name: joi.string().required(),
settings: joi.array().items(joi.object().unknown(true)).required(),
})
.unknown(true),
})
runJoi(validator, schema)
}
function validateDatasource(schema) {
const fieldValidator = joi.object({
type: joi
.string()
.allow(...Object.values(DatasourceFieldType))
.required(),
required: joi.boolean().required(),
default: joi.any(),
display: joi.string(),
})
const queryValidator = joi
.object({
type: joi.string().allow(...Object.values(QueryType)),
fields: joi.object().pattern(joi.string(), fieldValidator),
})
.required()
const validator = joi.object({
type: joi.string().allow("datasource").required(),
metadata: joi.object().unknown(true).required(),
hash: joi.string().optional(),
version: joi.string().optional(),
schema: joi.object({
docs: joi.string(),
friendlyName: joi.string().required(),
type: joi.string().allow(...DATASOURCE_TYPES),
description: joi.string().required(),
datasource: joi.object().pattern(joi.string(), fieldValidator).required(),
query: joi
.object({
create: queryValidator,
read: queryValidator,
update: queryValidator,
delete: queryValidator,
})
.unknown(true)
.required(),
}),
})
runJoi(validator, schema)
}
exports.validate = schema => {
switch (schema.type) {
case PluginTypes.COMPONENT:
validateComponent(schema)
break
case PluginTypes.DATASOURCE:
validateDatasource(schema)
break
default:
throw new Error(`Unknown plugin type - check schema.json: ${schema.type}`)
}
}

View File

@ -7,3 +7,5 @@ export interface FileType {
path: string
name: string
}
export const PLUGIN_TYPE_ARR = Object.values(exports.PluginType)