Skip to content

Instantly share code, notes, and snippets.

@droganov
Created February 25, 2018 17:19
Show Gist options
  • Save droganov/461087e71a1c35e8aeb39140d4b24d61 to your computer and use it in GitHub Desktop.
Save droganov/461087e71a1c35e8aeb39140d4b24d61 to your computer and use it in GitHub Desktop.
// Validator will pass a form to schema creator
export default form => ({
type: 'object',
properties: {
text: {
type: 'string',
messages: {
type: 'Should be a string',
required: 'Please enter a text',
},
},
minimum: {
type: 'integer',
// form data can be used to set schema props explicitly
maximum: form.maximum,
minimum: 0,
messages: {
type: 'Should be a zero or more',
minimum: 'Should exceed 0',
},
},
maximum: {
default: null,
type: ['integer', 'null'],
minimum: { $data: '1/minimum' },
// Messages key is not a part of json.org spec,
// but it is very useful in real life
messages: {
type: 'Should be a zero or more',
minimum: `Should exceed ${form.minimum || 0}`,
},
},
},
required: ['text', 'minimum'],
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment