Skip to content

Commit

Permalink
Release 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
joaonice committed Feb 13, 2020
1 parent 85ec775 commit b15aaa9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [v0.8.0](https://github.com/seegno/react-forms/tree/v0.8.0) (2020-02-13)
[Full Changelog](https://github.com/seegno/react-forms/compare/v0.7.0...v0.8.0)

**Merged pull requests:**

- Update validation to support custom keywords [\#22](https://github.com/seegno/react-forms/pull/22) ([bsonntag](https://github.com/bsonntag))
- Add validation options to validate custom formats [\#21](https://github.com/seegno/react-forms/pull/21) ([franciscomorais](https://github.com/franciscomorais))

## [v0.7.0](https://github.com/seegno/react-forms/tree/v0.7.0) (2019-12-05)
[Full Changelog](https://github.com/seegno/react-forms/compare/v0.6.0...v0.7.0)

Expand Down
6 changes: 4 additions & 2 deletions dist/components/form-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ var FormProvider = function FormProvider(props) {
jsonSchema = props.jsonSchema,
onFormValuesChanged = props.onFormValuesChanged,
onSubmit = props.onSubmit,
stateReducer = props.stateReducer;
stateReducer = props.stateReducer,
validationOptions = props.validationOptions;

var _useForm = (0, _useForm2["default"])({
initialValues: initialValues,
jsonSchema: jsonSchema,
onSubmit: onSubmit,
onValuesChanged: onFormValuesChanged,
stateReducer: stateReducer
stateReducer: stateReducer,
validationOptions: validationOptions
}),
fieldActions = _useForm.fieldActions,
formActions = _useForm.formActions,
Expand Down
31 changes: 24 additions & 7 deletions dist/hooks/use-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,26 @@ var metaReducer = function metaReducer(state, action) {
}
};
/**
* Errors reducer.
* `ErrorOptions` type.
*/


function errorsReducer(state, action, jsonSchema, values) {
/**
* Errors reducer.
*/
function errorsReducer(options) {
var action = options.action,
jsonSchema = options.jsonSchema,
state = options.state,
validationOptions = options.validationOptions,
values = options.values;

switch (action.type) {
case actionTypes.BLUR:
case actionTypes.SET_FIELD_VALUE:
case actionTypes.REGISTER_FIELD:
case actionTypes.SUBMIT_START:
return (0, _validate["default"])(jsonSchema, values);
return (0, _validate["default"])(jsonSchema, values, validationOptions);

case actionTypes.RESET:
return {};
Expand Down Expand Up @@ -189,13 +198,19 @@ function isSubmittingReducer(state, action) {
*/


var formReducer = function formReducer(jsonSchema, stateReducer) {
var formReducer = function formReducer(jsonSchema, validationOptions, stateReducer) {
return function (state, action) {
var fieldsValues = valuesReducer(state.fields.values, action);
var fieldsErrors = errorsReducer(state.fields.errors, action, jsonSchema, fieldsValues);
var fieldsMeta = metaReducer(state.fields.meta, action);
var isSubmitting = isSubmittingReducer(state.isSubmitting, action);
var submitStatus = submitStatusReducer(state.submitStatus, action);
var fieldsErrors = errorsReducer({
action: action,
jsonSchema: jsonSchema,
state: state.fields.errors,
validationOptions: validationOptions,
values: fieldsValues
});
var fieldsMetaValues = Object.values(fieldsMeta);
return stateReducer({
fields: {
Expand Down Expand Up @@ -234,9 +249,11 @@ function useForm(options) {
onSubmit = options.onSubmit,
onValuesChanged = options.onValuesChanged,
_options$stateReducer = options.stateReducer,
stateReducer = _options$stateReducer === void 0 ? _lodash.identity : _options$stateReducer;
stateReducer = _options$stateReducer === void 0 ? _lodash.identity : _options$stateReducer,
_options$validationOp = options.validationOptions,
validationOptions = _options$validationOp === void 0 ? {} : _options$validationOp;

var _useReducer = (0, _react.useReducer)(formReducer(jsonSchema, stateReducer), {
var _useReducer = (0, _react.useReducer)(formReducer(jsonSchema, validationOptions, stateReducer), {
fields: {
errors: {},
meta: {},
Expand Down
37 changes: 34 additions & 3 deletions dist/utils/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ Object.defineProperty(exports, "__esModule", {
});
exports["default"] = validate;

var _lodash = require("lodash");

var _ajv = _interopRequireDefault(require("ajv"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
Expand Down Expand Up @@ -85,11 +99,28 @@ function parseValidationErrors(validationErrors) {
*/


function validate(schema, values) {
var ajv = new _ajv["default"]({
function validate(schema, values, validateOptions) {
var _validateOptions;

var _ref = (_validateOptions = validateOptions) !== null && _validateOptions !== void 0 ? _validateOptions : {},
keywords = _ref.keywords,
restOptions = _objectWithoutProperties(_ref, ["keywords"]);

var ajv = new _ajv["default"]((0, _lodash.merge)({}, restOptions, {
$data: true,
allErrors: true
});
})); // TODO: Remove this when ajv adds support for passing keywords in the contructor.
// https://github.com/epoberezkin/ajv/issues/1136

if (keywords) {
for (var _i = 0, _Object$entries = Object.entries(keywords); _i < _Object$entries.length; _i++) {
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
keyword = _Object$entries$_i[0],
config = _Object$entries$_i[1];

ajv.addKeyword(keyword, config);
}
}

if (ajv.validate(schema, values)) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@seegno/react-forms",
"version": "0.7.0",
"version": "0.8.0",
"description": "",
"keywords": [
"form",
Expand Down

0 comments on commit b15aaa9

Please sign in to comment.