{"id":13660656,"url":"https://github.com/KernelPanic92/rx-form-mapper","last_synced_at":"2025-04-24T19:31:39.573Z","repository":{"id":35867437,"uuid":"187799893","full_name":"KernelPanic92/rx-form-mapper","owner":"KernelPanic92","description":"Proper decorator-based transformation / serialization / deserialization of plain javascript classes to angular reactive forms","archived":false,"fork":false,"pushed_at":"2025-04-23T16:05:34.000Z","size":549,"stargazers_count":24,"open_issues_count":20,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-23T17:23:42.773Z","etag":null,"topics":["angular","converter","decorator-pattern","deserialization","mapper","reactive-forms","serialization","typescript-library"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KernelPanic92.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-05-21T08:56:33.000Z","updated_at":"2025-01-14T23:59:40.000Z","dependencies_parsed_at":"2023-12-21T18:47:56.991Z","dependency_job_id":"54948274-e239-4811-b0c8-82a8b3f82db3","html_url":"https://github.com/KernelPanic92/rx-form-mapper","commit_stats":{"total_commits":130,"total_committers":3,"mean_commits":"43.333333333333336","dds":"0.49230769230769234","last_synced_commit":"4661e56a30c33b71c65d57baea710431b20302a8"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KernelPanic92%2Frx-form-mapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KernelPanic92%2Frx-form-mapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KernelPanic92%2Frx-form-mapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KernelPanic92%2Frx-form-mapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KernelPanic92","download_url":"https://codeload.github.com/KernelPanic92/rx-form-mapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250479759,"owners_count":21437424,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","converter","decorator-pattern","deserialization","mapper","reactive-forms","serialization","typescript-library"],"created_at":"2024-08-02T05:01:24.232Z","updated_at":"2025-04-24T19:31:34.565Z","avatar_url":"https://github.com/KernelPanic92.png","language":"TypeScript","readme":"\u003cimg src=\"https://github.com/KernelPanic92/rx-form-mapper/raw/master/resources/logo_big.png\"/\u003e\n\n[![codecov](https://codecov.io/gh/KernelPanic92/rx-form-mapper/branch/master/graph/badge.svg)](https://codecov.io/gh/KernelPanic92/rx-form-mapper)\n[![npm version](https://badge.fury.io/js/rx-form-mapper.svg)](https://badge.fury.io/js/rx-form-mapper)\n[![dependencies Status](https://david-dm.org/KernelPanic92/rx-form-mapper/status.svg)](https://david-dm.org/KernelPanic92/rx-form-mapper)\n[![NPM License](https://img.shields.io/npm/l/rx-form-mapper.svg)](https://img.shields.io/npm/l/rx-form-mapper.svg)\n[![NPM bundle size](https://img.shields.io/bundlephobia/min/rx-form-mapper.svg)](https://img.shields.io/bundlephobia/min/rx-form-mapper.svg)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\nRxFormMapper is a framework developed for angular and allows you to convert, by annotation, classes into reactive form and vice versa.\n\n## What is RxFormMapper\n\nReactive forms use an explicit and immutable approach to managing the state of a form at a given point in time. Each change to the form state returns a new state, which maintains the integrity of the model between changes. Reactive forms are built around observable streams, where form inputs and values are provided as streams of input values, which can be accessed synchronously.\n\nSo... Why RxFormMapper?\n\nSometimes you want to transform the classes you have into reactive forms, for example you have a user model that you want to have filled out by a form:\n\n```typescript\n\nexport class User {\n\tname: string;\n\tsurname: string;\n\tage: number;\n}\n\n```\n\nSo what to do? How to make a user form ? Solution is to create new instances of Reactive Form object and manually copy all properties to new object. But things may go wrong very fast once you have a more complex object hierarchy.\n\n```typescript\n\nnew FormGroup(\n\tname: new FormControl(user.name),\n\tsurname: new FormControl(user.surname),\n\tage: new FormControl(user.age),\n);\n\n```\n\nTo avoid all this you can use RxFormMapper: \n\n```typescript\n\nexport class User {\n\n\t@FormControl()\n\tname: string;\n\n\t@FormControl()\n\tsurname: string;\n\n\t@FormControl()\n\tage: number;\n}\n\n```\n\n```typescript\n\nimport { Component } from '@angular/core';\nimport { FormControl } from '@angular/forms';\nimport { User } from 'src/app/models/user.model';\n\n@Component({\n\tselector: 'app-user-editor',\n\ttemplateUrl: './user-editor.component.html',\n\tstyleUrls: ['./user-editor.component.css']\n})\nexport class UserEditorComponent {\n\n\tpublic form: FormGroup;\n\tconstructor(rxFormMapper: RxFormMapper) {\n\t\tthis.form = rxFormMapper.writeForm(User);\n\t}\n}\n\n```\n\n## Try it\n\nSee it in action at https://stackblitz.com/edit/rx-form-mapper-example?file=src/app/user-registration.ts\n\n## Getting started\n\n\n### Install npm package\n\n```bash\nnpm i rx-form-mapper --save\n\n```\n\n`reflect-metadata` is required (with angular+ you should already have this dependency installed.)\n\n```bash\nnpm i reflect-metadata --save\n\n```\n\n### Import the component modules\nImport the NgModule for RxFormMapper\n\n```typescript\nimport { RxFormMapperModule } from 'rx-form-mapper';\n\n@NgModule({\n  ...\n  imports: [RxFormMapperModule.forRoot()],\n  ...\n})\nexport class MyAppModule { }\n```\n\n### Inject RxFormMapper in your component\n\n```typescript\nimport { RxFormMapper } from 'rx-form-mapper';\n\n@Component({ ... })\nexport class MyComponent { \n\tconstructor(private readonly rxFormMapper: RxFormMapper) {}\n}\n```\n\n### Build your form\n\n```typescript\nimport { RxFormMapper } from 'rx-form-mapper';\nimport { Component } from '@angular/core';\nimport { FormGroup } from '@angular/forms';\nimport { User } from 'src/app/models/user.model';\n\n@Component({ ... })\nexport class MyComponent { \n\tpublic myForm: FormGroup;\n\tconstructor(rxFormMapper: RxFormMapper) {\n\t\tthis.myForm = rxFormMapper.writeForm(new User());\n\t}\n}\n```\n\n## Modules\n\n### RxFormMapperModule\n\nThis module enables RxFormMapper features\n\n## Services\n\n### RxFormMapper\n\nThis service provides the methods to serialize and deserialize our objects\n\n## Methods\n\n### writeForm\n\nThis method converts our class instance into reactive form instance\n\n```typescript\nthis.form = formMapper.writeForm(new Post());\n```\n\n### fromType\n\nThis method converts our class type into reactive form instance\n\n```typescript\nthis.form = formMapper.fromType(Post);\n```\n\n### readForm\n\nThis method converts our form instance into specific class instance\n\n```typescript\nconst post: Post = formMapper.readForm(this.form, Post);\n```\n\n## Decorators\n\n### @FormControl\n\nIf you want to expose some of properties as a FormControl, you can do it by @FormControl decorator\n\n```typescript\n\nimport { FormControl } from 'rx-form-mapper';\n\nexport class User {\n\n\t@FormControl()\n\tname: string;\n\n\t@FormControl()\n\tsurname: string;\n\n\t@FormControl()\n\tage: number;\n}\n\n```\n\n### @FormGroup\n\nIf you want to expose some of properties as a FormGroup, you can do it by @FormGroup decorator\n\n```typescript\n\nimport { FormGroup } from 'rx-form-mapper';\n\nexport class Child {}\n\nexport class User {\n\t@FormGroup()\n\tchild: Child;\n}\n\n```\n\n### @FormArray\n\nIf you want to expose some of properties as a FormArray, you can do it by @FormArray decorator\n\n```typescript\n\nimport { FormGroup } from 'rx-form-mapper';\n\nexport class Child {}\n\nexport class User {\n\t@FormArray(Child)\n\tchildren: Child[];\n}\n\n```\n\nWhen you're trying to serialize a property into FormArray its required to known what type of object you are trying to convert. \n\n### @Form\n\nIf you want to add extra data to your form, you can do it by optional @Form decorator\n\n```typescript\n\nimport { Form } from 'rx-form-mapper';\n\n@Form({\n\tvalidators: Validators.required\n})\nexport class User {\n\n\t@FormControl()\n\tname: string;\n\n\t@FormControl()\n\tsurname: string;\n\n\t@FormControl()\n\tage: number;\n}\n\n```\n\n### @CustomControl\n\nIf you want to create custom forms for specific fields, you can do it by @CustomControl decorator\n\nDeclare your custom mapper class implementing `CustomControlMapper` interface\n\n```typescript\n\nimport { CustomControlMapper } from 'rx-form-mapper';\nimport { AbstractControlOptions, FormControl } from '@angular/forms';\n\nexport class CustomAuthorControlMapper implements CustomControlMapper {\n\n\tpublic writeForm(value: any, abstractControlOptions: AbstractControlOptions): AbstractControl {\n\t\treturn new FormControl(value, abstractControlOptions);\n\t}\n\n\tpublic readForm(control: AbstractControl): ChildTestClass {\n\t\treturn control.value;\n\t}\n\n}\n\n```\n\nAnd pass it's type as argument of CustomControl decorator\n\n\n```typescript\n\nimport { Form } from 'rx-form-mapper';\nimport { CustomAuthorControlMapper } from '.';\n\nexport class Post {\n\n\t@CustomControl(CustomAuthorControlMapper)\n\tauthor: Person;\n\n}\n\n```\n\n## Injectable CustomMapper\n\nSometimes you want to injects other services into your CustomMapper, RxFormMapper allows you to do it simple:\n\nDeclare your CustomControlMapper class, decorate with `@Injectable` and includes it in a module as a normal service.\n\n```typescript\n\nimport { CustomControlMapper } from 'rx-form-mapper';\nimport { AbstractControlOptions, FormControl } from '@angular/forms';\n\n@Injectable()\nexport class CustomAuthorControlMapper implements CustomControlMapper {\n\n\tpublic writeForm(value: any, abstractControlOptions: AbstractControlOptions): AbstractControl {\n\t\treturn new FormControl(value, abstractControlOptions);\n\t}\n\n\tpublic readForm(control: AbstractControl): ChildTestClass {\n\t\treturn control.value;\n\t}\n\n}\n\n```\n\nAnd pass it's type as validator or asyncValidator option\n\n```typescript\n\nimport { Form } from 'rx-form-mapper';\nimport { CustomAuthorControlMapper } from '.';\n\nexport class Post {\n\n\t@CustomControl(CustomAuthorControlMapper)\n\tauthor: Person;\n\n}\n\n```\n\n## Validators\n\nIf you want to set a validator on a class or a property, you can do it by specifying `validators` option to `@Form`, `@FormControl`,`@CustomControl` or `@FormArray` decorators\n\n```typescript\n\nimport { FormControl } from 'rx-form-mapper';\n\nexport class User {\n\n\t@FormControl({\n\t\tvalidators: Validators.required\n\t})\n\tcompleteName: string;\n\n}\n\n```\n\n## Async validators\n\nIf you want to set an AsyncValidator on a class or a property, you can do it by specifying `asyncValidators` option to `@Form`, `@FormControl`,`@CustomControl` or `@FormArray` decorators\n\n```typescript\n\nimport { FormControl } from 'rx-form-mapper';\n\nconst asyncValidator = (control: AbstractControl) =\u003e return of(undefined);\n\nexport class User {\n\n\n\t@FormControl({\n\t\tasyncValidators: asyncValidator\n\t})\n\tname: string;\n\n}\n\n```\n\n## Injectable validators\n\nSometimes you want to injects other services into your validator or asyncValidator, RxFormMapper allows you to do it simple with Angular Forms interfaces:\n\nDeclare your validator class implementing `Validator` or `AsyncValidator` interfaces, decorate with `@Injectable` and includes it in a module as a normal service.\n\n```typescript\n\nimport { AsyncValidator } from '@angular/forms';\n\n@Injectable()\nexport class UniqueNameValidator implements AsyncValidator {\n\n\tconstructor(private readonly http: HttpProvider) {}\n\n\tpublic validate(control: AbstractControl): Promise\u003cValidationErrors\u003e | Observable\u003cValidationErrors\u003e {\n\t\t// implementation\n\t}\n\n}\n\n```\n\nAnd pass it's type as validator or asyncValidator option\n\n```typescript\n\nimport { FormControl } from 'rx-form-mapper';\nimport { UniqueNameValidator } from 'src/app/validators/unique-Name.validator';\n\nexport class User {\n\n\t@FormControl({\n\t\tasyncValidators: UniqueNameValidator\n\t})\n\tname: string;\n\n}\n\n```\n\n## Validation strategy\n\nSometimes you want to change the default strategy of form validation, you can do it specifying `updateOn` option to `@Form`, `@FormControl`,`@CustomControl` or `@FormArray` decorators\n\n```typescript\n\nimport { FormControl } from 'rx-form-mapper';\n\nexport class User {\n\n\t@FormControl({\n\t\tvalidators: Validators.required,\n\t\tupdateOn: 'blur'\n\t})\n\tname: string;\n\n}\n\n```\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKernelPanic92%2Frx-form-mapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKernelPanic92%2Frx-form-mapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKernelPanic92%2Frx-form-mapper/lists"}