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

Add support for validating functions inside schema's rules #2151

Conversation

Calyhre
Copy link
Collaborator

@Calyhre Calyhre commented Sep 4, 2018

Is this adding or improving a feature or fixing a bug?

Adding a feature

What's the new behaviour?

With this change we can now use validating functions inside schema's matching properties, at least for a few keys.

A few example:

{
  match: { /* something */ },
  object: kind => kind !== 'text',
  type: type => ['paragraph', 'image'].includes(type),
  data: data => data.has('thing'),
  marks: mark => mark.type === 'mention' && mark.data.has('userId'),
}

I also added documentation, specs, and default normalization on next and previous rules.

How does this change work?

A more complex one now allowed by this change:

{
  // Merge two consecutives lists
  match: { type: 'list' },
  next: {
    type: type => type !== 'list'
  },
  normalize: (change, error) => {
    if (error.code === 'next_sibling_invalid_type') {
      change.mergeNodeByKey(error.next);
    }
  },
}

Have you checked that...?

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn prettier.)
  • The relevant examples still work. (Run examples with yarn watch.)

Does this fix any issues or need any specific reviewers?

Fixes: #2139

@Calyhre Calyhre changed the title Feature/schema comparator functions Add support for validating functions inside schema's rules Sep 4, 2018
@ianstormtaylor
Copy link
Owner

Thank you @Calyhre, this is an absolutely perfect pull request!

@ianstormtaylor ianstormtaylor merged commit 405cef0 into ianstormtaylor:master Sep 4, 2018
@Calyhre Calyhre deleted the feature/schema-comparator-functions branch September 4, 2018 23:25
jtadmor pushed a commit to jtadmor/slate that referenced this pull request Jan 22, 2019
…aylor#2151)

* Add specs, defaults, and documentation to next/previous rule properties

* Add comparator function support for kind/object, type, data, and mark
z2devil pushed a commit to z2devil/slate that referenced this pull request Dec 6, 2024
…aylor#2151)

* Add specs, defaults, and documentation to next/previous rule properties

* Add comparator function support for kind/object, type, data, and mark
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add comparator functions on schema
2 participants