发布

  • fix(devkit): allow null values in JSON schema validation (#34167)

    frostbyte_neo 发布于 2026-01-23 20:09:09 +00:00

    Current Behavior

    Schema validation (done, for instance, when calling an executor) fails
    when an option has value "null" and schema accepts null values. I had it
    in a custom executor for nx-release-publish, that understands that
    nxReleaseVersionData is implicitly passed, so I define its schema:

    "newVersion": {
      "type": ["string", "null"],
      "description": "The new version of the project, null if no changes detected"
    }
    

    My code calls getReleaseClient().releaseVersion(options), which gets
    me a projectsVersionData object with version info. It contains null
    values (allowed). I then pass it, and ends up in:

     // nx/src/tasks-runner/task-orchestrator.ts:531-539                                                                                                                                                                      
      const combinedOptions = combineOptionsForExecutor(                                                                                                                                                                       
          task.overrides,  // ← Contains nxReleaseVersionData with null values                                                                                                                                                 
          task.target.configuration,                                                                                                                                                                                           
          targetConfiguration,                                                                                                                                                                                                 
          schema,           // ← Schema from executor                                                                                                                                                                          
          task.target.project,                                                                                                                                                                                                 
          relativeCwd,                                                                                                                                                                                                         
          isVerbose                                                                                                                                                                                                            
      );
    

    which fails inside:

     // nx/src/utils/params.js:126-201                                                                                                                                                                                        
      function validateObject(opts, schema, definitions) {                                                                                                                                                                     
          // Line 191-200: Iterate through all properties                                                                                                                                                                      
          Object.keys(opts).forEach((p) => {                                                                                                                                                                                   
              validateProperty(                                                                                                                                                                                                
                  p,                              // "nxReleaseVersionData"                                                                                                                                                    
                  opts[p],                        // { foo: { newVersion: null, ... }}                                                                                                                                         
                  (schema.properties ?? {})[p],   // schema for nxReleaseVersionData                                                                                                                                           
                  definitions                                                                                                                                                                                                  
              );                                                                                                                                                                                                               
          });                                                                                                                                                                                                                  
      }
    

    Expected Behavior

    null values should be considered, as they are valid in JSON schemas. It was probably not considered, because we never think that typeof null === "object", but it's unfortunately the case.

    Related Issue(s)

    I will create one

    Fixes https://github.com/nrwl/nx/issues/34169

    下载附件