@platformatic/foundation 3.15.0 → 3.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -101,6 +101,13 @@ export declare function replaceEnv (
101
101
  onMissingEnv?: (key: string) => string | undefined,
102
102
  ignore?: string[]
103
103
  ): RawConfiguration
104
+ export declare function validate (
105
+ schema: JSONSchemaType<any>,
106
+ config: RawConfiguration,
107
+ validationOptions?: object,
108
+ fixPaths?: boolean,
109
+ root?: string
110
+ ): void
104
111
  export declare function loadConfiguration (
105
112
  source: string | RawConfiguration,
106
113
  schema?: any,
package/lib/cli.js CHANGED
@@ -88,6 +88,7 @@ export function logo (color = true) {
88
88
  export function createCliLogger (level, noPretty) {
89
89
  let pretty
90
90
 
91
+ /* c8 ignore next 3 - Covered elsewhere */
91
92
  if (noPretty) {
92
93
  setPrettyPrint(false)
93
94
  } else {
@@ -316,6 +316,30 @@ export function createValidator (schema, validationOptions, context = {}) {
316
316
  return ajv.compile(schema)
317
317
  }
318
318
 
319
+ export function validate (schema, config, validationOptions = {}, fixPaths = true, root = '') {
320
+ const validator = createValidator(schema, validationOptions, { root, fixPaths })
321
+ const valid = validator(config)
322
+
323
+ if (!valid) {
324
+ const validationErrors = []
325
+ let errors = ':'
326
+
327
+ for (const validationError of validator.errors) {
328
+ /* c8 ignore next - else */
329
+ const path = validationError.instancePath === '' ? '/' : validationError.instancePath
330
+
331
+ validationErrors.push({ path, message: validationError.message, params: validationError.params })
332
+ errors += `\n - ${path}: ${validationError.message}`
333
+ }
334
+
335
+ const error = new ConfigurationDoesNotValidateAgainstSchemaError()
336
+ error.message += errors + '\n'
337
+ Object.defineProperty(error, 'validationErrors', { value: validationErrors })
338
+
339
+ throw error
340
+ }
341
+ }
342
+
319
343
  export async function loadEnv (root, ignoreProcessEnv = false, additionalEnv = {}) {
320
344
  if (!isAbsolute(root)) {
321
345
  root = resolve(process.cwd(), root)
@@ -411,7 +435,7 @@ export function replaceEnv (config, env, onMissingEnv, ignore) {
411
435
 
412
436
  export async function loadConfiguration (source, schema, options = {}) {
413
437
  const {
414
- validate,
438
+ validate: shouldValidate,
415
439
  validationOptions,
416
440
  transform,
417
441
  upgrade,
@@ -470,32 +494,12 @@ export async function loadConfiguration (source, schema, options = {}) {
470
494
  }
471
495
  }
472
496
 
473
- if (validate) {
497
+ if (shouldValidate) {
474
498
  if (typeof schema === 'undefined') {
475
499
  throw new SourceMissingError()
476
500
  }
477
501
 
478
- const validator = createValidator(schema, validationOptions, { root, fixPaths })
479
- const valid = validator(config)
480
-
481
- if (!valid) {
482
- const validationErrors = []
483
- let errors = ':'
484
-
485
- for (const validationError of validator.errors) {
486
- /* c8 ignore next - else */
487
- const path = validationError.instancePath === '' ? '/' : validationError.instancePath
488
-
489
- validationErrors.push({ path, message: validationError.message, params: validationError.params })
490
- errors += `\n - ${path}: ${validationError.message}`
491
- }
492
-
493
- const error = new ConfigurationDoesNotValidateAgainstSchemaError()
494
- error.message += errors + '\n'
495
- Object.defineProperty(error, 'validationErrors', { value: validationErrors })
496
-
497
- throw error
498
- }
502
+ validate(schema, config, validationOptions, fixPaths, root)
499
503
  }
500
504
 
501
505
  if (!skipMetadata) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/foundation",
3
- "version": "3.15.0",
3
+ "version": "3.16.0",
4
4
  "description": "Platformatic Foundation",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",