Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Length parameter for string values in schema #90

Closed
burhan85 opened this issue Oct 13, 2020 · 4 comments
Closed

Length parameter for string values in schema #90

burhan85 opened this issue Oct 13, 2020 · 4 comments

Comments

@burhan85
Copy link

I have modified the parser for this requirement for my own use, but an official change for a length parameter defined in the schema options would be very helpful.

@catamphetamine
Copy link
Owner

You mean, setting maxLength: number?
That would be too specific, I guess.
Though, an API could provide something like "custom types": maybe something like type: LimitedString where LimitedString would be an object providing type/parse and validate properties.

@burhan85
Copy link
Author

Yes, I mean providing a validation property for length in schema definition like
'Distributor Code': { prop: 'BuyerCode', type: String, maxLength: 45, required: true }
I had modified the js to validate the length but then went for a workaround by using 'parse'.
'Distributor Code': { prop: 'BuyerCode', type: String, required: true, parse(value) { if (value.length > 45) throw new Error('Distributor Code exceeds maximum length of 45 characters.'); else return value; } }
I don't get what you mean by being it too specific because it is kind of a basic thing to have a min/max length validation for strings and min/max value validation for numbers/dates. But yeah, I suppose, since this package is only for reading excel files so it might be an overkill to have too many validations.

@catamphetamine
Copy link
Owner

I don't get what you mean by being it too specific because it is kind of a basic thing to have a min/max length validation for strings and min/max value validation for numbers/dates. But yeah, I suppose, since this package is only for reading excel files so it might be an overkill to have too many validations.

Yeah, this package should not re-invent advanced validation: there already are things like yup for that. This package could support passing some kind of a validationSchema property that could be a yup schema, but then it would have to use it both for validation and parsing, and the original schema would only contain the prop property then, because other properties would be already contained in a yup schema, so the original schema could be simplified to something like { 'COLUMN_HEADER': 'propertyName', ... }, and it wouldn't be called a "schema" then, it would be called something like mapping, and schema would be the yup schema.

But I wouldn't implement something like that because that would be too much work.
Alternatively, this library could support the mapping property defined above, that would be an alternative to a basic schema with props and type: String for all props. Then, a developer could pass the output to something like yup for the actual parsing and validation.

@catamphetamine
Copy link
Owner

In version 4.1.0, a map parameter has been added: similar to schema but doesn't perform parsing or validation.
https://github.com/catamphetamine/read-excel-file#map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants