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

Support serialized fields transforming #245

Closed
kettanaito opened this issue Apr 15, 2018 · 2 comments
Closed

Support serialized fields transforming #245

kettanaito opened this issue Apr 15, 2018 · 2 comments
Assignees
Labels
enhancement Enhances existing functionality.
Projects
Milestone

Comments

@kettanaito
Copy link
Owner

kettanaito commented Apr 15, 2018

What

Add support to pre-format the serialized value of a field.

Why

This feature can be used in multiple scenarios:

  1. Encrypt the entered password upon serialization (before sending/accessing the data).
  2. Format date input value to match the desired date format.
  3. General value normalization (casing, characters stripping)

How

  • As a part of createField options (not preferable)
  • Would be nice to have it as serialize method on the instance of a custom component wrapped in createField

Specification

General statements

  1. Serialization formatting affects only the returned serialized value. It has no affect over the actual field's value, or field's state, in general.
  2. It is possible to provide a custom serialization formatter in two ways:
    1. Upon the field's composition.
    2. As a dedicated prop to the field's instance.
  3. When no custom formatter function is provided, a field's value is returned during the serialization (as it is now)

Syntax

type SerializeFunc<ValueType> = (value: ValueType) => ValueType
@kettanaito kettanaito added the enhancement Enhances existing functionality. label Apr 15, 2018
@kettanaito kettanaito added this to the 1.x.x milestone Apr 15, 2018
@kettanaito kettanaito added this to In scope in Roadmap Apr 15, 2018
@kettanaito
Copy link
Owner Author

kettanaito commented May 17, 2018

Draft

Specification

  • Field's serialized value can be controlled using onSerialize prop of the respective field, or upon the field's construction via createField options.
  • onSerialize prop accepts a function that returns the serialized field's value
  • onSerialize function can return nothing, which makes the field skipped during the serialization

Usage

This:

export default createField({
  onSerialize({ value, fieldProps, fields, form }) {
    return value.replace(' ', '') // removes spaces
  }
})(Input)

This implies that all instances of Input field will have the provided serialized value transformer.

And this:

<Input
  name="fieldOne"
  value="some thing"
  onSerialize={({ value, fieldProps, fields, form }) => {
    return value.replace(' ', '') // removes spaces
  }} />

This implies that only this very Input field has the provided serialized value transformer.

Both declaration examples will be serialized into the following JSON:

{
  "fieldOne": "something"
}

@kettanaito
Copy link
Owner Author

kettanaito commented Aug 29, 2018

  • Need to adjust flattenDeep to properly handle plain objects. There is an issue with passing iterable instances, as it would iterate deeply through its private keys (i.e. __ID).

flattenDeep was removed. Used getLeavesWhich instead.

@kettanaito kettanaito self-assigned this Aug 30, 2018
@kettanaito kettanaito moved this from In scope to In progress in Roadmap Aug 30, 2018
@kettanaito kettanaito changed the title Field: Support serialized value formatting Support serialized fields transforming Sep 11, 2018
Roadmap automation moved this from In progress to Released Sep 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhances existing functionality.
Projects
Roadmap
  
Released
Development

No branches or pull requests

1 participant