-
Notifications
You must be signed in to change notification settings - Fork 120
Struggling with Enums type validation when value is not an existing property of the map #335
Description
Ciao, @gcanti. I hope to not be a bother with this issue. I'm not sure if this is the right place to open it since it involves other tcomb dependencies/repos.
Version
3.2.29
Expected behaviour
I am currently using a custom template to render a react-select/creatable for my form select inputs. I want the user to be able to search for an item in the select list, or add a new entry so when the form is submitted, next time the/a new user fills in the form, the new entry will be in said list.
Actual behaviour
I have no issues with the render of the component itself, nor getting the select (actually a combination of text input + select) current value. The problem comes when submitting the form and the form validation is triggered:
{
"type": "string",
"label": "My searchable input",
"enum": [ the list of objects I want to use for the searchable select ]
}
Tcomb will check if the selected value in the select exists in the list provided to the component. (enums.js -> Enum.is function). Since the select value is a new value, map.hasOwnProperty(new value) will fail and return false, therefore making Enum.is return false.
I am complete beginner to tcomb and I am struggling to understand how it works along with other repos such as tcomb-form, tcomb-form-native, tcomb-validation, etc. As I have seen in tcomb-json-schema,
var types = {
string: function(s) {
if (s.hasOwnProperty('enum')) {
if (t.Array.is(s['enum'])) {
return t.enums.of(s['enum']);
} else {
return t.enums(s['enum']);
}
}
Could you guys clarify what's going on under the hood, from the moment I get the value of the input till it validates? And how can I override the enum type so the validation omits that "Enum.is -> map.hasOwnProperty()"? I'm trying to connect the dots and get a better understanding of this powerful tool that is tcomb.
I've tried to take other similar issues as reference, as well as read the readme provided by canti, this is what my "custom" type looks like:
import t from 'tcomb-form-native/lib'
const searchable = t.subtype(t.String, () => true, 'searchable')
searchable.getTcombFormFactory = () => t.form.Select
{
"type": "searchable",
"label": "My searchable input",
"enum": [ the list of objects I want to use for the searchable select ]
}
Stack trace and console log
In tcomb-form-native, components.js
class Form extends React.Component {
pureValidate() {
function Enums(value, path) {
assert(Enums.is(value)...
}
"NewProvider2" being the new entry to the list (value of the select when the form was submitted)
Thank you all in advance and have a good one 👍