Skip to content

Commit af076c7

Browse files
committed
Add tv4 validation from ASF into core
1 parent 6dee9fc commit af076c7

File tree

6 files changed

+299
-154
lines changed

6 files changed

+299
-154
lines changed

bundle.js renamed to dist/json-schema-form-core.js

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/*!
22
* json-schema-form
3-
* @version 1.0.0-alpha.1
3+
* @version 1.0.0-alpha.2
44
* @link https://github.com/json-schema-form/json-schema-form-core
55
* @license MIT
66
* Copyright (c) 2016 JSON Schema Form
77
*/
88
(function webpackUniversalModuleDefinition(root, factory) {
99
if(typeof exports === 'object' && typeof module === 'object')
10-
module.exports = factory();
10+
module.exports = factory(require("tv4"));
1111
else if(typeof define === 'function' && define.amd)
12-
define([], factory);
12+
define(["tv4"], factory);
1313
else {
14-
var a = factory();
14+
var a = typeof exports === 'object' ? factory(require("tv4")) : factory(root["tv4"]);
1515
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
1616
}
17-
})(this, function() {
17+
})(this, function(__WEBPACK_EXTERNAL_MODULE_10__) {
1818
return /******/ (function(modules) { // webpackBootstrap
1919
/******/ // The module cache
2020
/******/ var installedModules = {};
@@ -111,7 +111,19 @@ return /******/ (function(modules) { // webpackBootstrap
111111
});
112112
});
113113

114-
var _schemaDefaults = __webpack_require__(9);
114+
var _validate = __webpack_require__(9);
115+
116+
Object.keys(_validate).forEach(function (key) {
117+
if (key === "default") return;
118+
Object.defineProperty(exports, key, {
119+
enumerable: true,
120+
get: function get() {
121+
return _validate[key];
122+
}
123+
});
124+
});
125+
126+
var _schemaDefaults = __webpack_require__(11);
115127

116128
var schemaDefaultsImp = _interopRequireWildcard(_schemaDefaults);
117129

@@ -563,6 +575,81 @@ return /******/ (function(modules) { // webpackBootstrap
563575

564576
'use strict';
565577

578+
Object.defineProperty(exports, "__esModule", {
579+
value: true
580+
});
581+
exports.validate = validate;
582+
583+
var _tv = __webpack_require__(10);
584+
585+
var _tv2 = _interopRequireDefault(_tv);
586+
587+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
588+
589+
/**
590+
* Validate a value against its form definition and schema.
591+
* The value should either be of proper type or a string, some type
592+
* coercion is applied.
593+
*
594+
* @param {Object} form A merged form definition, i.e. one with a schema.
595+
* @param {Any} value the value to validate.
596+
* @return {Object} a tv4js result object.
597+
*/
598+
function validate(form, value) {
599+
if (!form) {
600+
return { valid: true };
601+
};
602+
603+
var schema = form.schema;
604+
if (!schema) {
605+
return { valid: true };
606+
};
607+
608+
// Input of type text and textareas will give us a viewValue of ''
609+
// when empty, this is a valid value in a schema and does not count as something
610+
// that breaks validation of 'required'. But for our own sanity an empty field should
611+
// not validate if it's required.
612+
if (value === '') {
613+
value = undefined;
614+
};
615+
616+
// Numbers fields will give a null value, which also means empty field
617+
if (form.type === 'number' && value === null) {
618+
value = undefined;
619+
};
620+
621+
// Version 4 of JSON Schema has the required property not on the
622+
// property itself but on the wrapping object. Since we like to test
623+
// only this property we wrap it in a fake object.
624+
var wrap = { type: 'object', 'properties': {} };
625+
var propName = form.key[form.key.length - 1];
626+
wrap.properties[propName] = schema;
627+
628+
if (form.required) {
629+
wrap.required = [propName];
630+
};
631+
632+
var valueWrap = {};
633+
if (!!value) {
634+
valueWrap[propName] = value;
635+
};
636+
637+
return _tv2.default.validateResult(valueWrap, wrap);
638+
} /* Common code for validating a value against its form and schema definition */
639+
;
640+
641+
/***/ },
642+
/* 10 */
643+
/***/ function(module, exports) {
644+
645+
module.exports = tv4;
646+
647+
/***/ },
648+
/* 11 */
649+
/***/ function(module, exports, __webpack_require__) {
650+
651+
'use strict';
652+
566653
Object.defineProperty(exports, "__esModule", {
567654
value: true
568655
});

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import canonicalTitleMapImp from './lib/canonicalTitleMap';
55
export * from './lib/merge.js';
66
export * from './lib/select.js';
77
export * from './lib/traverse.js';
8+
export * from './lib/validate.js';
89

910
export const sfPath = sfPathImp;
1011
export const schemaDefaults = schemaDefaultsImp;

lib/validate.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Common code for validating a value against its form and schema definition */
2+
import tv4 from 'tv4';
3+
4+
/**
5+
* Validate a value against its form definition and schema.
6+
* The value should either be of proper type or a string, some type
7+
* coercion is applied.
8+
*
9+
* @param {Object} form A merged form definition, i.e. one with a schema.
10+
* @param {Any} value the value to validate.
11+
* @return {Object} a tv4js result object.
12+
*/
13+
export function validate(form, value) {
14+
if (!form) {
15+
return { valid: true };
16+
};
17+
18+
var schema = form.schema;
19+
if (!schema) {
20+
return { valid: true };
21+
};
22+
23+
// Input of type text and textareas will give us a viewValue of ''
24+
// when empty, this is a valid value in a schema and does not count as something
25+
// that breaks validation of 'required'. But for our own sanity an empty field should
26+
// not validate if it's required.
27+
if (value === '') {
28+
value = undefined;
29+
};
30+
31+
// Numbers fields will give a null value, which also means empty field
32+
if (form.type === 'number' && value === null) {
33+
value = undefined;
34+
};
35+
36+
// Version 4 of JSON Schema has the required property not on the
37+
// property itself but on the wrapping object. Since we like to test
38+
// only this property we wrap it in a fake object.
39+
var wrap = { type: 'object', 'properties': {}};
40+
var propName = form.key[form.key.length - 1];
41+
wrap.properties[propName] = schema;
42+
43+
if (form.required) {
44+
wrap.required = [ propName ];
45+
};
46+
47+
var valueWrap = {};
48+
if (!!value) {
49+
valueWrap[propName] = value;
50+
};
51+
52+
return tv4.validateResult(valueWrap, wrap);
53+
};

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "json-schema-form-core",
3-
"version": "1.0.0-alpha.1",
3+
"version": "1.0.0-alpha.2",
44
"description": "Core library",
5-
"main": "bundle.js",
5+
"main": "dist/json-schema-form-core.js",
66
"scripts": {
77
"build": "webpack",
88
"prepublish": "npm run build",
@@ -34,6 +34,7 @@
3434
"babel-preset-es2015": "^6.6.0",
3535
"chai": "^3.5.0",
3636
"mocha": "^2.4.5",
37+
"tv4": "^1.2.7",
3738
"webpack": "^1.12.14"
3839
},
3940
"licenses": [

0 commit comments

Comments
 (0)