forked from tsai/budibase
4 changed files with 197 additions and 7 deletions
@ -0,0 +1,66 @@ |
|||||
|
import { recursivelyValidate } from "./validateProps"; |
||||
|
import { |
||||
|
isString, |
||||
|
keys, |
||||
|
flatten, |
||||
|
isArray, |
||||
|
map, |
||||
|
filter |
||||
|
} from "lodash/fp"; |
||||
|
import { common } from "budibase-core"; |
||||
|
const pipe = common.$; |
||||
|
|
||||
|
export const validatePage = (page, getComponent) => { |
||||
|
const errors = []; |
||||
|
const error = message => errors.push(message); |
||||
|
|
||||
|
const noIndex = !page.index || !page.index._component; |
||||
|
if(noIndex) { |
||||
|
error("Must choose a component for your index.html"); |
||||
|
} |
||||
|
|
||||
|
if(!page.appBody |
||||
|
|| !isString(page.appBody) |
||||
|
|| !page.appBody.endsWith(".json")) { |
||||
|
error("App body must be set toa valid JSON file"); |
||||
|
} |
||||
|
|
||||
|
const indexHtmlErrors = noIndex |
||||
|
? [] |
||||
|
: pipe( |
||||
|
recursivelyValidate(page.index, getComponent), [ |
||||
|
map(e => `Index.html: ${e.error}`) |
||||
|
]); |
||||
|
|
||||
|
return [...errors, ...indexHtmlErrors]; |
||||
|
} |
||||
|
|
||||
|
export const validatePages = (pages, getComponent) => { |
||||
|
|
||||
|
let errors = []; |
||||
|
const error = message => errors.push(message); |
||||
|
|
||||
|
if(!pages.main) { |
||||
|
error("must have a 'main' page"); |
||||
|
} |
||||
|
|
||||
|
if(!pages.unauthenticated) { |
||||
|
error("must have a 'unauthenticated' (login) page"); |
||||
|
} |
||||
|
|
||||
|
if(!pages.componentLibraries |
||||
|
|| !isArray(pages.componentLibraries) |
||||
|
|| pages.componentLibraries.length === 0) { |
||||
|
|
||||
|
error("componentLibraries must be set to a non-empty array of strings"); |
||||
|
} |
||||
|
|
||||
|
const pageErrors = pipe(pages, [ |
||||
|
keys, |
||||
|
filter(k => k !== "componentLibraries"), |
||||
|
map(k => validatePage(pages[k], getComponent)), |
||||
|
flatten |
||||
|
]); |
||||
|
|
||||
|
return [...errors, ...pageErrors]; |
||||
|
} |
||||
@ -0,0 +1,92 @@ |
|||||
|
import { |
||||
|
validatePages, |
||||
|
validatePage |
||||
|
} from "../src/userInterface/propsDefinitionParsing/validatePages"; |
||||
|
|
||||
|
const validPages = () => ({ |
||||
|
"main" : { |
||||
|
"index" : { |
||||
|
"_component": "testIndexHtml", |
||||
|
"title": "My Cool App", |
||||
|
"customScripts": [ |
||||
|
{"url": "MyCustomComponents.js"} |
||||
|
] |
||||
|
}, |
||||
|
"appBody" : "./main.app.json" |
||||
|
}, |
||||
|
"unauthenticated" : { |
||||
|
"index" : { |
||||
|
"_component": "testIndexHtml", |
||||
|
"title": "My Cool App - Login", |
||||
|
"customScripts": [ |
||||
|
{"url": "MyCustomComponents.js"} |
||||
|
] |
||||
|
}, |
||||
|
"appBody" : "./unauthenticated.app.json" |
||||
|
}, |
||||
|
"componentLibraries": ["./myComponents"] |
||||
|
}); |
||||
|
|
||||
|
const getComponent = name => ({ |
||||
|
testIndexHtml : { |
||||
|
title: "string", |
||||
|
customScripts: { |
||||
|
type:"array", |
||||
|
elementDefinition: { |
||||
|
url: "string" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
}[name]) |
||||
|
|
||||
|
describe("validate single page", () => { |
||||
|
|
||||
|
it("should return no errors when page is valid", () => { |
||||
|
const errors = validatePage( |
||||
|
validPages().main, |
||||
|
getComponent); |
||||
|
|
||||
|
expect(errors).toEqual([]); |
||||
|
}); |
||||
|
|
||||
|
it("should return error when index is not set, or set incorrectly", () => { |
||||
|
let page = validPages().main; |
||||
|
delete page.index; |
||||
|
expect(validatePage(page, getComponent).length).toEqual(1); |
||||
|
|
||||
|
page.index = {title:"something"}; // no _component
|
||||
|
const noComponent = validatePage(page, getComponent); |
||||
|
expect(noComponent.length).toEqual(1); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
it("should return error when appBody is not set, or set incorrectly", () => { |
||||
|
let page = validPages().main; |
||||
|
delete page.appBody; |
||||
|
expect(validatePage(page, getComponent).length).toEqual(1); |
||||
|
|
||||
|
page.appBody = true; // not a string
|
||||
|
expect(validatePage(page, getComponent).length).toEqual(1); |
||||
|
|
||||
|
page.appBody = "something.js"; // not a json
|
||||
|
expect(validatePage(page, getComponent).length).toEqual(1); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe("validate pages", () => { |
||||
|
|
||||
|
it("should return no errors when pages are valid", () => { |
||||
|
const errors = validatePages( |
||||
|
validPages(), |
||||
|
getComponent); |
||||
|
|
||||
|
expect(errors).toEqual([]); |
||||
|
}); |
||||
|
|
||||
|
it("should return error when index is not set, or set incorrectly", () => { |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
}); |
||||
Loading…
Reference in new issue