generated from ubiquity-os/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1208f25
commit aba3449
Showing
3 changed files
with
56 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { TransformDecodeCheckError, TransformDecodeError, Value, ValueError } from "@sinclair/typebox/value"; | ||
import { Env, envValidator, PluginSettings, pluginSettingsSchema, pluginSettingsValidator } from "../types"; | ||
|
||
export function validateAndDecodeSchemas(env: Env, rawSettings: object) { | ||
const errors: ValueError[] = []; | ||
const settings = Value.Default(pluginSettingsSchema, rawSettings) as PluginSettings; | ||
|
||
if (!pluginSettingsValidator.test(settings)) { | ||
for (const error of pluginSettingsValidator.errors(settings)) { | ||
console.error(error); | ||
errors.push(error); | ||
} | ||
} | ||
|
||
if (!envValidator.test(env)) { | ||
for (const error of envValidator.errors(env)) { | ||
console.error(error); | ||
errors.push(error); | ||
} | ||
} | ||
|
||
if (errors.length) { | ||
throw { errors }; | ||
} | ||
|
||
try { | ||
const decodedEnv = Value.Decode(envValidator.schema, env); | ||
const decodedSettings = Value.Decode(pluginSettingsSchema, settings); | ||
return { decodedEnv, decodedSettings }; | ||
} catch (e) { | ||
if (e instanceof TransformDecodeCheckError || e instanceof TransformDecodeError) { | ||
throw { errors: [e.error] }; | ||
} | ||
throw e; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters