Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Validate that default value is string or numeric #45

Closed
wants to merge 1 commit into from

2 participants

@alex-seville

Right now there is an implicit* validation done after the read (https://github.com/flatiron/prompt/blob/master/lib/prompt.js#L463), but I think it might be helpful to do an explicit validation beforehand.

  • implicit only because it's not strictly testing the default value, unless the default is being returned
@indexzero
Owner

@alex-seville typeof defaultLine !== 'numeric' is not valid javascript. I believe you want typeof defaultLine !== 'number'

@alex-seville
@alex-seville alex-seville Validate that default value is string or numeric. Ignore otherwise.
Fixed typo

wrote numeric instead of number

If default is not string, try casting it

Number gets ignored on the way back anyway, so it's best to string converting it to a string
3c3c731
@alex-seville

I made a mess of the commits, so I squashed them.

I also noticed that number values would be ignored after reading by the same code on L463, so I modified the code to try doing a toString() to non-string values (to convert numbers). That way it's string in/string out.

@alex-seville

P.S. Read v.1.0.3 won't accept objects or arrays as a default value (https://github.com/isaacs/read/blob/master/lib/read.js#L11)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 12, 2012
  1. @alex-seville

    Validate that default value is string or numeric. Ignore otherwise.

    alex-seville authored
    Fixed typo
    
    wrote numeric instead of number
    
    If default is not string, try casting it
    
    Number gets ignored on the way back anyway, so it's best to string converting it to a string
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 0 deletions.
  1. +3 −0  lib/prompt.js
View
3  lib/prompt.js
@@ -390,6 +390,9 @@ prompt.getInput = function (prop, callback) {
schema = convert(schema);
defaultLine = schema.default;
+ if (typeof defaultLine !== 'string'){
+ defaultLine = defaultLine.toString ? defaultLine.toString() : null;
+ }
name = prop.description || schema.description || propName;
raw = prompt.colors
? [prompt.message, delim + name.grey, delim.grey]
Something went wrong with that request. Please try again.