mozilla/node-convict

coerce send too many (useless) argument

A-312 opened this issue · 3 comments

A-312 commented

Depending of the README, coerce should be have only one argument but we send 3 arguments, coerce: function(val) {. I will simplify the script for #313 and I remove the 2th and 3th arg.

convict.addFormat({
  name: 'float-percent',
  validate: function(val) {
    if (val !== 0 && (!val || val > 1 || val < 0)) {
      throw new Error('must be a float between 0 and 1, inclusive');
    }
  },
  coerce: function(val) {
    console.log(arguments, +val);
    return parseFloat(val, 10);
  }
});

Current log :

[Arguments] {                                                       
  '0': '0.8',                                                       
  '1': {                                                            
    getArgs: [Function: getArgs],                                   
    getEnv: [Function: getEnv],                                     
    getProperties: [Function: getProperties],                       
    toString: [Function: toString],                                 
    getSchema: [Function: getSchema],                               
    getSchemaString: [Function: getSchemaString],                   
    get: [Function: get],                                           
    default: [Function: default],                                   
    reset: [Function: reset],                                       
    has: [Function: has],                                           
    set: [Function: set],                                           
    load: [Function: load],                                         
    loadFile: [Function: loadFile],                                 
    validate: [Function: validate],                                 
    _def: { ip: [Object], port: [Object], percent: [Object] },      
    _schema: { properties: [Object] },                              
    _env: { IP_ADDRESS: [Array], PORT: [Array], percent: [Array] }, 
    _argv: {},                                                      
    _sensitive: Set {},                                             
    _instance: { ip: '127.0.0.1', port: 8080, percent: 0.1, ok: 8 } 
  },                                                                
  '2': 'percent'                                                    
} 0.8                                                               

Expected :

[Arguments] {                                                       
  '0': '0.8'                                               
} 0.8   
A-312 commented

Solution 2 depending of #262/#263 :

Add doc about the 2th and 3th arg BUT #313 should permit todo the same thing of #262.

A-312 commented

Ok... Coerce can be used for placeholder #110 (the doc don't explain that, the explain was removed).

A-312 commented

(the doc don't explain that, the explain was removed)

812cd15

Remove coerce advanced usage
since code too complicated already and only works in limited cases
Source: @madarche's commit

"only works in limited cases" => Then, this code should be not in convict (and see my explain #110 (comment) , I say the same thing, placeholder only work with a perfect order during the recursive function..., this code is like a hack not a solution).