You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为cz-customizable配置必填提交问题及校验规则?

如何用cz-customizable设置Commit必填项与校验规则

可以通过cz-customizable的内置配置项和自定义校验函数,实现提交项的必填设置与规则校验,以下是针对你需求的具体实现方案:

1. 提交类型(type)的必填保障

提交类型本身是选择式提问,用户必须从预设的types列表中选择一个选项,无法留空。如果你的配置中设置了defaultType(当前为"style"),工具会默认选中该类型;若要强制用户手动选择类型而非使用默认值,可将defaultType设为undefined:

defaultType: undefined,

2. 作用域(scope)设为必填

当前你的配置中allowEmptyScopes: true,允许用户选择空作用域。若要强制必填,只需将该值改为false:

allowEmptyScopes: false,

修改后,用户必须从scopes列表(当前为test1、test2)中选择一个作用域,无法选择"empty"选项。如果需要允许用户自定义作用域,可保持allowCustomScopes: true,但仍会禁止空值提交。

3. 提交描述(subject)设为必填并校验长度

当前minSubjectLength: 0允许空描述,若要禁止空值并限制最小长度,修改该参数为大于0的数值(例如5):

minSubjectLength: 5,

这样用户必须输入至少5个字符的描述才能继续提交。你也可以通过maxSubjectLength和subjectLimit设置描述的长度上限。

4. 自定义复杂校验规则

如果需要更灵活的校验逻辑(例如特定类型必须搭配特定作用域、描述格式校验等),可以添加customValidate配置项,编写自定义校验函数:

customValidate: (answers) => {
  const errors = [];
  // 示例1:修复bug(fix类型)必须选择test1作为作用域
  if (answers.type === 'fix' && answers.scope !== 'test1') {
    errors.push('修复bug时必须选择test1作为作用域');
  }
  // 示例2:提交描述不能以小写字母开头(符合命令式语气要求)
  if (answers.subject && answers.subject[0].toLowerCase() === answers.subject[0]) {
    errors.push('提交描述需以大写字母开头,使用命令式语气(如"Add login validation")');
  }
  return errors;
},

当校验不通过时,函数返回的错误信息会逐一提示给用户,用户必须修正后才能完成提交。

修改后的完整配置示例

module.exports = {
  messages: {
    type: "Select the type of change that you're committing:",
    scope: "Denote the SCOPE of this change:",
    customScope: "Denote the SCOPE of this change:",
    subject: "Write a SHORT, IMPERATIVE tense description of the change:\n",
    body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n',
    breaking: 'List any BREAKING CHANGES (optional). Use "|" to break new line:\n',
    footerPrefixesSelect: "Select the ISSUES type of changeList by this change (optional):",
    customFooterPrefix: "Input ISSUES prefix:",
    footer: "List any ISSUES by this change. E.g.: #31, #34:\n",
    generatingByAI: 'Generating your AI commit subject...',
    generatedSelectByAI: 'Select suitable subject by AI generated:',
    confirmCommit: "Are you sure you want to proceed with the commit above?"
  },
  types: [
    { value: "feat", name: "feat:     ✨  A new feature", emoji: ":sparkles:" },
    { value: "chore", name: "chore:    🔨  Other changes that don't modify src or test files", emoji: ":hammer:" },
    { value: "fix", name: "fix:      🩹💩  A bug fix", emoji: ":bug:" },
    { value: "test", name: "test:     ✅  Adding missing tests or correcting existing tests", emoji: ":white_check_mark:" },
    { value: "style", name: "style:    💄  Changes that do not affect the meaning of the code", emoji: ":lipstick:" },
    { value: "refactor", name: "refactor: 🔧   A code change that neither fixes a bug nor adds a feature", emoji: ":recycle:" },
    { value: "docs", name: "docs:     📝  Documentation only changes", emoji: ":memo:" },
    { value: "perf", name: "perf:     🚀  A code change that improves performance", emoji: ":zap:" },
    { value: "build", name: "build:    📦️   Changes that affect the build system or external dependencies", emoji: ":package:" },
    { value: "ci", name: "ci:       🎡  Changes to our CI configuration files and scripts", emoji: ":ferris_wheel:" },
    { value: "revert", name: "revert:   ⏪️  Reverts a previous commit", emoji: ":rewind:" }
  ],
  useEmoji: true,
  emojiAlign: "center",
  useAI: false,
  aiNumber: 1,
  themeColorCode: "",
  scopes: [{name: 'test1'}, {name: 'test2'}],
  allowCustomScopes: false,
  allowEmptyScopes: false, // 修改为必填
  customScopesAlign: "bottom",
  customScopesAlias: "custom",
  emptyScopesAlias: "empty",
  upperCaseSubject: true,
  markBreakingChangeMode: true,
  allowBreakingChanges: ['feat', 'fix'],
  breaklineNumber: 5,
  breaklineChar: "|",
  skipQuestions: ['breaking', 'customScope', 'breaking'],
  issuePrefixes: [{ value: "closed", name: "closed:   ISSUES has been processed" }],
  customIssuePrefixAlign: "top",
  emptyIssuePrefixAlias: "skip",
  customIssuePrefixAlias: "custom",
  allowCustomIssuePrefix: true,
  allowEmptyIssuePrefix: true,
  confirmColorize: true, 
  maxHeaderLength: Infinity,
  maxSubjectLength: Infinity,
  minSubjectLength: 5, // 设置最小长度,禁止空值
  scopeOverrides: undefined,
  defaultType: undefined, // 强制用户手动选择类型
  defaultBody: "",
  defaultIssues: "",
  defaultScope: "",
  defaultSubject: "",
  subjectLimit: 100,
  // 添加自定义校验
  customValidate: (answers) => {
    const errors = [];
    if (answers.type === 'fix' && answers.scope !== 'test1') {
      errors.push('修复bug时必须选择test1作为作用域');
    }
    if (answers.subject && answers.subject[0].toLowerCase() === answers.subject[0]) {
      errors.push('提交描述需以大写字母开头,使用命令式语气');
    }
    return errors;
  }
}

内容的提问来源于stack exchange,提问作者Siavash Bashiri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 21:00:10