serverless/serverless · error · Error

Unexpected non-function AJV validator type. Please report at

Error message

Unexpected non-function AJV validator type. Please report at https://github.com/serverless/serverless including all the logs output

What it means

Thrown after loading a previously-generated AJV standalone validator from the on-disk cache. resolve-ajv-validate.js reads the cached JS module via requireFromString and checks that the result is a function (the validate function). If it is not a function (e.g., undefined, an object, or a corrupted module), it logs the unexpected value and source code, then throws a plain Error. This is an internal integrity check — it should never fire under normal conditions.

Source

Thrown at packages/serverless/lib/classes/config-schema-handler/resolve-ajv-validate.js:101

    await fsp.writeFile(tmpCachePath, moduleCode)
    await safeMoveFile(tmpCachePath, cachePath)
    await fsp.rmdir(tmpDir)
  }

  await ensureExists(cachePath, generate)
  const loadedModuleCode = await fsp.readFile(cachePath, 'utf-8')
  const validator = requireFromString(
    loadedModuleCode,
    path.resolve(__dirname, `[generated-ajv-validate]${filename}`),
  )

  if (typeof validator !== 'function') {
    log.error(
      'Unexpected validator %o, resolved from source %s',
      validator,
      loadedModuleCode,
    )
    throw new Error(
      'Unexpected non-function AJV validator type. Please report at https://github.com/serverless/serverless including all the logs output',
    )
  }

  cachedValidatorsBySchemaHash[schemaHash] = validator
  return validator
}

export default getValidate

View on GitHub (pinned to b9d7ea51c8)

Solutions

  1. Delete the AJV validate cache: rm -rf ~/.serverless/artifacts/ajv-validate-* and re-run — the framework will regenerate the validator.
  2. If the issue persists, set SLS_SCHEMA_CACHE_BASE_DIR to a writable temp directory to rule out permission issues.
  3. Report the issue at https://github.com/serverless/serverless with the logged source code output.
  4. Check for concurrent Serverless processes that might race on the same cache directory.

Example fix

# before — corrupted cache causes non-function validator
# (error on deploy)

# after — clear cache and retry
rm -rf ~/.serverless/artifacts/ajv-validate-*
serverless deploy
Defensive patterns

Strategy: fallback

Validate before calling

// Before running, check if the cache directory is writable and clean
const fs = require('fs')
const path = require('path')
const os = require('os')
const cacheDir = path.join(os.homedir(), '.serverless', 'artifacts')
try {
  fs.accessSync(cacheDir, fs.constants.W_OK)
} catch {
  console.warn('AJV cache dir not writable; consider setting SLS_SCHEMA_CACHE_BASE_DIR')
}

Try / catch

// Wrap framework invocation; on cache corruption, clean and retry
const { execSync } = require('child_process')
try {
  execSync('serverless deploy', { stdio: 'inherit' })
} catch (e) {
  if (e.stderr && e.stderr.includes('Unexpected non-function AJV validator')) {
    execSync('rm -rf ~/.serverless/artifacts/ajv-validate-*')
    execSync('serverless deploy', { stdio: 'inherit' })
  } else {
    throw e
  }
}

Prevention

When it happens

Trigger: The cached validator file at ~/.serverless/artifacts/ajv-validate-<date>/<hash>.js is corrupted, truncated, or overwritten by another process. requireFromString returns a non-function (partial module, empty file, or a module whose default export was lost). Can also occur if the AJV standalone code generation produced an invalid module due to an AJV version mismatch.

Common situations: A crashed or interrupted run left a half-written cache file. Another tool or antivirus modified files in the .serverless cache directory. Disk corruption. An AJV version bump changed the standalone code output format while old cached modules remain.

Related errors


AI-assisted analysis of serverless/serverless@b9d7ea51c8 (2026-08-13). Data as JSON: /api/errors/ac5e355f53e8d30e. Report an issue: GitHub.