Skip to content

Commit

Permalink
Update README with yup example
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Apr 18, 2022
1 parent ba3a71a commit fa355f2
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,67 @@ Other available types include the following. Please put in a PR if you need more
import { ValidationResult, ValidatorMapFunc, ValidatorAction } from 'ember-changeset/types';
```

## Alternative Changeset

Enabled in 4.1.0

We now ship a ValidatedChangeset that is a proposed new API we would like to ship. The goal of this refactor is to remove confusing APIs and externalize validations.

- ✂️ `save`
- ✂️ `cast`
- ✂️ `merge`
- `errors` are required to be added to the Changeset manually after `validate`
- `validate` takes a callback with the sum of changes. In user land you will call `changeset.validate((changes) => yupSchema.validate(changes))`

```js
import Component from '@glimmer/component';
import { ValidatedChangeset } from 'ember-changeset';
import { action, get } from '@ember/object';
import { object, string } from 'yup';

class Foo {
user = {
name: 'someone',
email: '[email protected]',
};
}

const FormSchema = object({
cid: string().required(),
user: object({
name: string().required(),
email: string().email(),
})
});

export default class ValidatedForm extends Component {
constructor() {
super(...arguments);

this.model = new Foo();
this.changeset = ValidatedChangeset(this.model);
}

@action
async setChangesetProperty(path, evt) {
this.changeset.set(path, evt.target.value);
try {
await this.changeset.validate((changes) => FormSchema.validate(changes));
this.changeset.removeError(path);
} catch (e) {
this.changeset.addError(e.path, { value: this.changeset.get(e.path), validation: e.message });
}
}

@action
async submitForm(changeset, event) {
event.preventDefault();

changeset.execute();
}
}
```

## API

- Properties
Expand Down

0 comments on commit fa355f2

Please sign in to comment.