-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d9f991
commit bfd463f
Showing
9 changed files
with
349 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
"license": "MIT", | ||
"version": "0.0.0-development", | ||
"author": "Claudio Savino <[email protected]> (https://twitter.com/foxhound87)", | ||
"description": "Automagically manage React forms state and automatic validation with MobX.", | ||
"description": "Reactive MobX Form State Management", | ||
"homepage": "https://github.com/foxhound87/mobx-react-form#readme", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
|
@@ -95,6 +95,7 @@ | |
"eslint": "^8.35.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"husky": "0.13.1", | ||
"joi": "^17.13.3", | ||
"json-loader": "0.5.4", | ||
"lodash-webpack-plugin": "^0.11.6", | ||
"mobx": "^6.3.3", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import _ from "lodash"; | ||
import { | ||
ValidationPlugin, | ||
ValidationPluginConfig, | ||
ValidationPluginConstructor, | ||
ValidationPluginInterface, | ||
} from "../models/ValidatorInterface"; | ||
|
||
class JOI implements ValidationPluginInterface { | ||
promises = []; | ||
|
||
config = null; | ||
|
||
state = null; | ||
|
||
extend = null; | ||
|
||
validator = null; | ||
|
||
schema = null; | ||
|
||
constructor({ | ||
config, | ||
state = null, | ||
promises = [], | ||
}: ValidationPluginConstructor) { | ||
this.state = state; | ||
this.promises = promises; | ||
this.extend = config?.extend; | ||
this.validator = config.package; | ||
this.schema = config.schema; | ||
this.extendValidator(); | ||
} | ||
|
||
extendValidator(): void { | ||
// extend using "extend" callback | ||
if (typeof this.extend === "function") { | ||
this.extend({ | ||
validator: this.validator, | ||
form: this.state.form, | ||
}); | ||
} | ||
} | ||
|
||
validate(field): void { | ||
const { error } = this.schema.validate(field.state.form.validatedValues, { abortEarly: false }); | ||
if (!error) return; | ||
|
||
const fieldPathArray = field.path.split('.'); | ||
|
||
const fieldErrors = error.details | ||
.filter(detail => { | ||
const errorPathString = detail.path.join('.'); | ||
const fieldPathString = fieldPathArray.join('.'); | ||
return errorPathString === fieldPathString || errorPathString.startsWith(`${fieldPathString}.`); | ||
}) | ||
.map(detail => { | ||
// Replace the path in the error message with the custom label | ||
const label = detail.context?.label || detail.path.join('.'); | ||
const message = detail.message.replace(`${detail.path.join('.')}`, label); | ||
return message; | ||
}); | ||
|
||
if (fieldErrors.length) { | ||
field.validationErrorStack = fieldErrors; | ||
} | ||
} | ||
} | ||
|
||
export default (config?: ValidationPluginConfig): ValidationPlugin => ({ | ||
class: JOI, | ||
config, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { expect } from "chai"; | ||
import j from "joi"; | ||
import FormInterface from "../../../../src/models/FormInterface"; | ||
import OptionsModel from "../../../../src/models/OptionsModel"; | ||
import { Form } from "../../../../src"; | ||
import joi from "../../../../src/validators/JOI"; | ||
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface"; | ||
|
||
|
||
const fields = [ | ||
"user.username", | ||
"user.email", | ||
"user.password", | ||
"user.passwordConfirm", | ||
]; | ||
|
||
const values = { | ||
user: { | ||
username: 'a', | ||
email: 'notAValidEmail@', | ||
password: 'x', | ||
passwordConfirm: 'mysecretpassword', | ||
} | ||
} | ||
|
||
const schema = j.object({ | ||
user: j.object({ | ||
username: j.string().min(3).required().label('Username'), | ||
email: j.string().email().required(), | ||
password: j.string().min(6).max(25).required(), | ||
passwordConfirm: j.string().min(6).max(25).valid(j.ref('password')).required().messages({ | ||
'any.only': 'Passwords do not match', | ||
'any.required': 'Password confirmation is required', | ||
}), | ||
}).required() | ||
}); | ||
|
||
const plugins: ValidationPlugins = { | ||
joi: joi({ | ||
package: j, | ||
schema, | ||
}), | ||
}; | ||
|
||
const options: OptionsModel = { | ||
validateOnInit: true, | ||
showErrorsOnInit: true, | ||
}; | ||
|
||
export default new Form({ | ||
fields, | ||
values, | ||
}, { | ||
plugins, | ||
options, | ||
name: "Nested-Z3", | ||
hooks: { | ||
onInit(form: FormInterface) { | ||
describe("Check joi validation flag", () => { | ||
it('user.username hasError should be true', () => expect(form.$('user.username').hasError).to.be.true); | ||
it('user.email hasError should be true', () => expect(form.$('user.email').hasError).to.be.true); | ||
it('user.password hasError should be true', () => expect(form.$('user.password').hasError).to.be.true); | ||
it('user.passwordConfirm hasError should be true', () => expect(form.$('user.passwordConfirm').hasError).to.be.true); | ||
}); | ||
|
||
describe("Check joi validation errors", () => { | ||
it('user.username error should equal joi error', () => expect(form.$('user.username').error).to.be.equal('"Username" length must be at least 3 characters long')); | ||
it('user.email error should equal joi error', () => expect(form.$('user.email').error).to.be.equal('"user.email" must be a valid email')); | ||
it('user.password error should equal joi error', () => expect(form.$('user.password').error).to.be.equal('"user.password" length must be at least 6 characters long')); | ||
it('user.passwordConfirm error should equal joi error', () => expect(form.$('user.passwordConfirm').error).to.be.equal('Passwords do not match')); | ||
}); | ||
|
||
} | ||
} | ||
}); |
Oops, something went wrong.