Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ const wpDeployer = async () => {
console.error(chalk.red(`Invalid SVN URL: ${errorMessage}`))
return EXIT_CONFIG
}
if (error === 'invalid_config') {
console.error(chalk.red(errorMessage || 'Invalid wpDeployer configuration.'))
return EXIT_CONFIG
}

try {
runPreflightSync(settings, { fs, awk })
Expand Down
62 changes: 62 additions & 0 deletions lib/config-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* JSON Schema validation for merged wp-deployer settings (via Ajv).
* Domain rules JSON Schema cannot express stay in lib/validators and config.js.
*/

import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import Ajv from 'ajv'

const __dirname = dirname(fileURLToPath(import.meta.url))

const schema = JSON.parse(
readFileSync(join(__dirname, 'schemas', 'config.schema.json'), 'utf8')
)

const ajv = new Ajv({ allErrors: true, strict: true })
const validate = ajv.compile(schema)

/**
* @param {object} settings - Merged settings object
* @returns {{ ok: true } | { ok: false, error: 'username_required' | 'theme_earlier_version_required' | 'invalid_config', errorMessage?: string }}
*/
export function validateSettingsSchema (settings) {
const valid = validate(settings)
if (valid) return { ok: true }

const errors = validate.errors ?? []
const mapped = mapSchemaErrors(errors, settings)
if (mapped) return { ok: false, ...mapped }

return {
ok: false,
error: 'invalid_config',
errorMessage: ajv.errorsText(errors, { separator: '\n' })
}
}

/**
* Preserve stable error codes used by the CLI where they match a schema failure.
* @param {import('ajv').ErrorObject[]} errors
* @param {object} settings
* @returns {{ error: string, errorMessage?: string } | null}
*/
function mapSchemaErrors (errors, settings) {
for (const e of errors) {
const path = e.instancePath ?? ''
const missing = e.params?.missingProperty

if (missing === 'username' || (path === '/username' && e.keyword === 'minLength')) {
return { error: 'username_required' }
}

if (settings.repoType === 'theme') {
if (missing === 'earlierVersion' || (path === '/earlierVersion' && e.keyword === 'minLength')) {
return { error: 'theme_earlier_version_required' }
}
}
}

return null
}
20 changes: 11 additions & 9 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import merge from 'just-merge'
import { validateSettingsSchema } from './config-schema.js'
import { validateSvnSafeVersion, validateSvnUrl } from './validators/index.js'
import { hasPathUnsafeChars } from './validators/shared.js'

Expand All @@ -30,7 +31,7 @@ export function getDefaults (pkg) {
/**
* Resolve settings from package.json. Does not exit; returns an error key if validation fails.
* @param {object} pkg - Parsed package.json (must have name, version)
* @returns {{ settings: object, error: null | 'invalid_slug' | 'username_required' | 'theme_earlier_version_required' | 'invalid_svn_url' | 'invalid_new_version' | 'invalid_earlier_version', errorMessage?: string }}
* @returns {{ settings: object, error: null | 'invalid_slug' | 'username_required' | 'theme_earlier_version_required' | 'invalid_config' | 'invalid_svn_url' | 'invalid_new_version' | 'invalid_earlier_version', errorMessage?: string }}
*/
export function resolveSettings (pkg) {
const defaults = getDefaults(pkg)
Expand Down Expand Up @@ -74,6 +75,15 @@ export function resolveSettings (pkg) {
}
}

const schemaResult = validateSettingsSchema(settings)
if (!schemaResult.ok) {
return {
settings,
error: schemaResult.error,
...(schemaResult.errorMessage !== undefined && { errorMessage: schemaResult.errorMessage })
}
}

const urlResult = validateSvnUrl(settings.url)
if (!urlResult.ok) {
return {
Expand All @@ -84,14 +94,6 @@ export function resolveSettings (pkg) {
}
settings.url = urlResult.value

if (!settings.username) {
return { settings, error: 'username_required' }
}

if (!settings.earlierVersion && settings.repoType === 'theme') {
return { settings, error: 'theme_earlier_version_required' }
}

const newVersionResult = validateSvnSafeVersion(settings.newVersion)
if (!newVersionResult.ok) {
return {
Expand Down
48 changes: 48 additions & 0 deletions lib/schemas/config.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://github.com/ernilambar/wp-deployer/lib/schemas/config.schema.json",
"title": "wp-deployer merged settings",
"description": "Schema for wp-deployer runtime configuration.",
"type": "object",
"required": [
"url",
"slug",
"mainFile",
"username",
"repoType",
"buildDir",
"maxBuffer",
"deployTrunk",
"deployTag",
"deployAssets",
"assetsDir",
"tmpDir",
"earlierVersion",
"newVersion",
"svnPath"
],
"properties": {
"url": { "type": "string" },
"slug": { "type": "string", "minLength": 1 },
"mainFile": { "type": "string", "minLength": 1 },
"username": { "type": "string", "minLength": 1 },
"repoType": { "type": "string", "enum": ["plugin", "theme"] },
"buildDir": { "type": "string", "minLength": 1 },
"maxBuffer": { "type": "integer", "minimum": 1 },
"deployTrunk": { "type": "boolean" },
"deployTag": { "type": "boolean" },
"deployAssets": { "type": "boolean" },
"assetsDir": { "type": "string", "minLength": 1 },
"tmpDir": { "type": "string", "minLength": 1 },
"earlierVersion": { "type": "string" },
"newVersion": { "type": "string", "minLength": 1 },
"svnPath": { "type": "string", "minLength": 1 }
},
"additionalProperties": false,
"if": { "properties": { "repoType": { "const": "theme" } } },
"then": {
"properties": {
"earlierVersion": { "type": "string", "minLength": 1 }
}
}
}
106 changes: 85 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"homepage": "https://github.com/ernilambar/wp-deployer#readme",
"dependencies": {
"ajv": "^8.18.0",
"chalk": "^5.6.2",
"fs-extra": "^11.3.4",
"just-merge": "^3.2.0"
Expand Down
20 changes: 20 additions & 0 deletions test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,24 @@ describe('resolveSettings', () => {
const { error } = resolveSettings(pkg)
assert.strictEqual(error, 'invalid_slug')
})

it('returns invalid_config when merged settings fail JSON Schema (wrong type)', () => {
const pkg = {
...basePkg,
wpDeployer: { username: 'jane', maxBuffer: 'not-a-number' }
}
const { error, errorMessage } = resolveSettings(pkg)
assert.strictEqual(error, 'invalid_config')
assert.ok(errorMessage && errorMessage.length > 0)
})

it('returns invalid_config when wpDeployer sets unknown properties (additionalProperties)', () => {
const pkg = {
...basePkg,
wpDeployer: { username: 'jane', notARealOption: true }
}
const { error, errorMessage } = resolveSettings(pkg)
assert.strictEqual(error, 'invalid_config')
assert.ok(errorMessage && errorMessage.length > 0)
})
})