Releases: heshansw/simplemap
1.3.1
Reorder Nested Object Array (Available with v1.3.0)
Now you can reorder nested object array on ASC or DESC order and return reordered object in same structure as the original object
-
reOrder Reorder nested object array
-
Parameters
- originalObject Original Object
- keyPath Nested Key Path to be reordered
- SortOrder Sorting Order (SortOrder.ASC for Ascending and SortOrder.DESC for descending)
Lets say you have this data set
{ "schoolName":"Doe School", "address":"Stockholm", "classes":[ { "className":"Class 01", "year":"Year 01", "studentData":{ "students":[ { "studentName":"Philip Larry", "age":16 }, { "studentName":"John Doe", "age":18 }, { "studentName":"Alex Numan", "age":15 }, { "studentName":"Joe Max", "age":20 }, { "studentName":"Remy Max", "age":21 } ] } }, { "className":"Class 02", "year":"Year 01", "studentData":{ "students":[ { "studentName":"Bale Larry", "age":16 }, { "studentName":"John Doe", "age":18 }, { "studentName":"Andersson", "age":15 }, { "studentName":"Joe Max", "age":20 } ] } } ] }You can get reordered result by using below method with nested key path to be reordered as the 2nd paramater
const reordered = reOrder(schoolData, 'classes.studentData.students.studentName')
So studentName reordered object will look like below
{ "schoolName":"Doe School", "address":"Stockholm", "classes":[ { "className":"Class 01", "year":"Year 01", "studentData":{ "students":[ { "studentName":"Alex Numan", "age":15 }, { "studentName":"Joe Max", "age":20 }, { "studentName":"John Doe", "age":18 }, { "studentName":"Philip Larry", "age":16 }, { "studentName":"Remy Max", "age":21 } ] } }, { "className":"Class 02", "year":"Year 01", "studentData":{ "students":[ { "studentName":"Andersson", "age":15 }, { "studentName":"Bale Larry", "age":16 }, { "studentName":"Joe Max", "age":20 }, { "studentName":"John Doe", "age":18 } ] } } ] }
-
1.3.0
1.2.3
1.2.2
1.2.1
Improvement with v1.2.1
With minor version update, you can use new attribute called decimal. If an input field contains attribute **decimal**, form submission will validate input field and if input field contains non decimal value, form submission will trigger an error
```html
<input name="amount" decimal />
```
If there is an error with decimal, you can see error like
```json
{ "name": "DECIMAL_ERROR", "message": "amount" }
```
Message will contain error input field name
What's Changed
Full Changelog: 1.2.0...1.2.1
1.2.0
- new getFormData method to directly map form data to an object using form submit event
- email validation will trigger inside getFormData method if there are input type email input fields
Form Value Mappers (Available with v1.2.0)
-
getFormData Can map form data directly to object using this method. If form has an input type with email, this method will use our own validateEmail method and validate the email input value.
-
Parameters
- event Submit Event
Need to pass form Submit event.
Lets say you have below mentioned form (Please note that this example uses React. But this feature can be used with any framework/library or with vanilla Typescript / Javascript).
But input name is required<form id="formDataModel" onSubmit="{handleSubmit}"> <input placeholder="name" required name="name" type="text" /> <input placeholder="email" required name="email" type="email" /> <input placeholder="grade" name="grade" type="text" /> <button type="submit">Submit</button> </form>
So this object has an invalid email address. So this will return Error.
type EmailUser = { name: string email: EmailBasic } handleSubmit = (event: SubmitEvent) => { try { const formData = getFormData(event) as EmailUser } catch (ex) { console.error(ex) } }
Please wrap this method with a try catch block. If there is an issue with form data validations, the event will go to catch block.
for an example below error will be triggered on invalid email addressError: INVALID_EMAILFor this example, when the form is submitted, formData will be like below,
{ "name": "John", "email": "john@example.com", "grade": "A" }Further improvements on this method and form related improvements will be there will next versions.
-
1.1.1
1.1.0
Minor Feature Improvement with Email Validations
With this version now you can use email validations to validate emails inside objects and string emails
Email Validators (Available with v1.1.0)
-
validateEmail Check given email address is valid..
-
Parameters
- email Email Address (EmailBasic type which is coming with this package)
Assume we have this email:
test@test.comYou can use below mentod to validate this email address
const validateSingleEmail = validateEmail('test@test.com')
-
-
EmailBasic type
from this custom basic type, you can validate basic email on development -
getEmail Check given email address is valid.
-
Parameters
- email Email Address (EmailBasic type which is coming with this package)
Assume we have this email:
test@test.comYou can use below mentod to validate this email address
const validateSingleEmail = validateEmail('test@test.com')
if email is valid, email will be returned. otherwise error exception will get returned.
-
-
validateEmailObject In Same way you can use this method which will return the entire object, if the email address is valid
-
Parameters
- obj Original Object
- path key path
You can use below mentod to validate object email
try { return validateEmailObject(userData, 'contact.email') } catch (ex) { return ex }
if email is valid, email will be returned. otherwise error exception will get returned.
-
-
autoValidateEmail Validate an object if the object contains key which contains word 'email'
-
Parameters
- obj Original Object
Lets say you have an object which needs to be validated if the object has any key value with key contains word 'email'. Then you can use this method.
Lets say you have below mentioned object
const userDataError: User = { name: 'heshan', contact: { email: 'test@#.c', address: 'Malmö', }, }
So this object has an invalid email address. So this will return Error.
try { return autoValidateEmail(userData) } catch (ex) { return ex }
if email is valid, email will be returned. otherwise error exception will get returned.
Error: INVALID_EMAIL
-