diegohaz / schm

Composable schemas for JavaScript and Node.js

Home Page:https://git.io/schm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Specify type: Array, parse returns string value

J-F-Liu opened this issue · comments

e.g.

schema({
  correctOptions: {
    type: Array,
    minlength: 1,
  },
});
commented

Hi, @J-F-Liu. Array types are currently only supported in this form:

schema({
  correctOptions: {
    type: [String], // or other type
    minlength: 1,
  },
});

But it would be nice to support type: Array. If you or someone else wants to work on that, you can start by updating these lines: https://github.com/diegohaz/schm/blob/master/packages/schm/src/parsers.js#L15-L27

  switch (option.name) {
    case "RegExp":
      return new RegExp(value, "i");
    case "Date":
      return new Date(/^\d{5,}$/.test(value) ? Number(value) : value);
    case "Boolean":
      return !(value === "false" || value === "0" || !value);
    case "Number":
      return Number(value);
    case "Object":
      return Object(value);
    default:
      return String(value);
      ^^^^^^^^^^^^^^^^^^^^
  }

I think the default case should return the original value instead of convert to string.

schema({
  correctOptions: {
    type: [String], // or other type
    minlength: 1,
  },
});

with this code, the minlength validator is ignored.

commented

Will open another issue for that problem.