Skip to content

Instantly share code, notes, and snippets.

@Jyrno42
Last active July 7, 2017 09:57
Show Gist options
  • Save Jyrno42/3dbfc54f7df0454cfb90e9bbf9371e59 to your computer and use it in GitHub Desktop.
Save Jyrno42/3dbfc54f7df0454cfb90e9bbf9371e59 to your computer and use it in GitHub Desktop.
[Proposal] tg-resources ValidationError changes
# Key differences w/ the current default error parser
- Lists of errors joined by a comma
+ Lists of primitive errors joined by a space (configurable via options)
+ Lists of complex errors parsed as ListValidationErrors
+ ValidationError.asString()
+ ValidationError.toString() (proxy of asString)
+ Add iterators to ValidationErrors w/ children (so users don't have to iterate over .errors directly). Note: List/Object validationerrors should have same iteration API
const responseBody = {
statusCode: 400,
responseText: JSON.stringify({
errors: {
non_field_errors: [ // string (joined by space)
'Something is generally broken',
],
password: [ // string (joined by space)
'too short.',
'missing numbers.',
],
email: { // Nested (object) ValidationError
something: 'be wrong yo', // string
},
remember: false, // string
deliveryAddress: [ // Nested (list) ValidationError
{ // Nested (object) ValidationError
non_field_errors: [
'Provided address is not supported', // string (joined by space)
],
},
null, // null
{ // Nested (object) ValidationError
zip: 'Please enter a valid address', // string
country: [ // string (joined by space)
'This field is required.',
'Please select a valid country.'
]
},
undefined, // null (since no errors)
{ // null (since no errors)
non_field_errors: [],
},
{} // null (since no errors)
],
paymentMethods: [] // null
},
}),
};
const parsedError = ValidationError({
nonFieldErrors: 'Something is generally broken',
errors: {
password: 'Too short. missing numbers.',
email: ValidationError({
nonFieldErrors: null,
errors: {
something: 'be wrong yo',
},
}),
remember: 'false',
deliveryAddress: ListValidationError({
nonFieldErrors: undefined, // unused for list validation errors (e.g. always undefined)
errors: [
ValidationError({
nonFieldErrors: 'Provided address is not supported',
errors: {},
}),
null,
ValidationError({
nonFieldErrors: null,
errors: {
zip: 'Please enter a valid address',
country: 'This field is required. Please select a valid country.',
}
}),
null,
null,
null,
],
}),
// paymentMethods is stripped since it evaluates to an empty array (those are ignored)
}
})
@Jyrno42
Copy link
Author

Jyrno42 commented Jul 7, 2017

I also have been thinking of adding a SingleValidationError class which would responsible for single validation errors. It would also feature an utility function for string conversion (mby toString by default). This new utility should be added to all ValidationError types.

With SingleValidationError we would have turtles all the way down (e.g. each item would be ?ValidatonError type).

@simonschmidt
Copy link

simonschmidt commented Jul 7, 2017

Preserving non_field_errors as list might make it easier to display in a meaningful way so unrelated sentences don't end up looking like a paragraph (user can still join with space if they want to)

parsedError.nonFieldErrors.map(e => <div class="error">e</div>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment