Scrubber
When Slate throws an exception, it includes a stringified representation of the relevant data. For example, if your application makes an API call to access the child of a text Node (an impossible operation), Slate will throw an exception like this:
1
Cannot get the child of a text node: {"text": "This is my text node."}
Copied!
If your rich text editor can include sensitive customer data, you may want to scrub or obfuscate that text. To help with that, you can use the Scrubber API. Here's an example of recursively scrubbing the 'text' fields of any entity that gets logged.
1
import { Scrubber } from 'slate'
2
3
Scrubber.setScrubber((key, value) => {
4
if (key === 'text') return '... scrubbed ...'
5
return value
6
})
Copied!
By setting the scrubber in this way, the error example given above will be printed as
1
Cannot get the child of a text node: {"text": "... scrubbed ..."}
Copied!

Text Randomizer Example

Here's an example "textRandomizer" scrubber, which randomizes particular fields of Nodes, preserving their length, but replacing their contents with randomly chosen alphanumeric characters.
1
import { Scrubber } from 'slate'
2
3
const textRandomizer = (fieldNames: string[]) => (key, value) => {
4
if (fieldNames.includes(key)) {
5
if (typeof value === 'string') {
6
return value
7
.split('')
8
.map(generateRandomCharacter)
9
.join('')
10
} else {
11
return '... scrubbed ...'
12
}
13
}
14
15
return value
16
}
17
18
const generateRandomCharacter = (): string => {
19
const chars =
20
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ1234567890'
21
return chars.charAt(Math.floor(Math.random() * chars.length))
22
}
23
24
// randomize the 'text' and 'src' fields of any Node that is included in an
25
// exception thrown by Slate
26
Scrubber.setScrubber(Scrubber.textRandomizer(['text', 'src']))
Copied!
In this example, a Node that looked like
1
{ "text": "My test input string", "count": 5 }
Copied!
will be logged by Slate in an exception as (the random string will differ):
1
{ "text": "rSIvEzKe39l6rqQSCfyv", "count": 5 }
Copied!
Copy link