Skip to content

Commit

Permalink
Fix PR #605 error and create test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthropic committed Feb 4, 2017
1 parent fa96f5a commit e52a3f2
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 27 deletions.
2 changes: 1 addition & 1 deletion dist/angular-schema-form-bootstrap.js
@@ -1,7 +1,7 @@
/*!
* angular-schema-form
* @version 1.0.0-alpha.1
* @date Tue, 31 Jan 2017 11:57:55 GMT
* @date Sat, 04 Feb 2017 11:08:45 GMT
* @link https://github.com/json-schema-form/angular-schema-form
* @license MIT
* Copyright (c) 2014-2017 JSON Schema Form
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-schema-form-bootstrap.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions dist/angular-schema-form.js
@@ -1,7 +1,7 @@
/*!
* angular-schema-form
* @version 1.0.0-alpha.1
* @date Tue, 31 Jan 2017 13:48:26 GMT
* @date Sat, 04 Feb 2017 11:08:45 GMT
* @link https://github.com/json-schema-form/angular-schema-form
* @license MIT
* Copyright (c) 2014-2017 JSON Schema Form
Expand Down Expand Up @@ -2153,10 +2153,11 @@ FIXME: real documentation
childScope.schemaForm = { form: merged, schema: schema };

//clean all but pre existing html.
Array.prototype.forEach.call(element[0].children, function (child) {
if ([' ', child.className, ' '].join('').indexOf(' schema-form-ignore ') === -1 && child.querySelectorAll('[sf-insert-field]').length === 0) {
__WEBPACK_IMPORTED_MODULE_0_angular___default.a.element(child).remove();
}
Array.prototype.forEach.call(element.children(), function (child) {
var jchild = __WEBPACK_IMPORTED_MODULE_0_angular___default.a.element(child);
if (false === jchild.hasClass('schema-form-ignore')) {
jchild.remove();
};
});

// Find all slots.
Expand Down
6 changes: 3 additions & 3 deletions dist/angular-schema-form.min.js

Large diffs are not rendered by default.

70 changes: 57 additions & 13 deletions src/directives/schema-validate.directive.spec.js
Expand Up @@ -8,6 +8,19 @@ describe('directive', function() {
})
);

tv4.defineError('EMAIL', 10001, 'Invalid email address');
tv4.defineKeyword('email', function(data, value, schema) {
if (schema.email) {
if (/^\S+@\S+$/.test(data)) {
return null;
}
return {
code: 10001
};
}
return null;
});

exampleSchema = {
"type": "object",
"title": "Person",
Expand Down Expand Up @@ -71,19 +84,6 @@ describe('directive', function() {
);

inject(function($compile,$rootScope) {
tv4.defineError('EMAIL', 10001, 'Invalid email address');
tv4.defineKeyword('email', function(data, value, schema) {
if (schema.email) {
if (/^\S+@\S+$/.test(data)) {
return null;
}
return {
code: 10001
};
}
return null;
});

var scope = $rootScope.$new();
scope.obj = { "email": "NULL" };

Expand Down Expand Up @@ -111,4 +111,48 @@ describe('directive', function() {
form.$valid.should.be.false;
});
});

it('should allow custom tv4 error default message to be set', function() {
//TODO test message rename
// app.config(['sfErrorMessageProvider', function(sfErrorMessageProvider) {
// sfErrorMessageProvider.setDefaultMessage(10001, 'Whoa! Can you double check that email address for me?');
// }]);

tmpl = angular.element(
'<div>' +
'<form name="testform" sf-schema="schema" sf-form="form" sf-model="obj"></form>' +
'{{obj}}' +
'</div>'
);

inject(function($compile,$rootScope) {

var scope = $rootScope.$new();
scope.obj = { "email": "NULL" };

scope.schema = exampleSchema;

scope.form = [
{
"key": "email",
"placeholder": "Enter contact email",
"feedback": false
},
{
"type": "submit",
"style": "btn-info",
"title": "OK"
}
];

$compile(tmpl)(scope);
$rootScope.$apply();

var form = tmpl.find('form').eq(0).controller('form');

form.$valid.should.be.true;
tmpl.find('input.btn-info').click();
//TODO form.$valid.should.be.false;
});
});
});
9 changes: 5 additions & 4 deletions src/directives/sf-schema.directive.js
Expand Up @@ -100,10 +100,11 @@ sfSelect, sfPath, sfBuilder) {
childScope.schemaForm = { form: merged, schema: schema };

//clean all but pre existing html.
Array.prototype.forEach.call(element[0].children, function(child) {
if (([' ', child.className, ' '].join('')).indexOf(' schema-form-ignore ') === -1 && child.querySelectorAll('[sf-insert-field]').length === 0) {
angular.element(child).remove();
}
Array.prototype.forEach.call(element.children(), function(child) {
let jchild = angular.element(child);
if (false === jchild.hasClass('schema-form-ignore')) {
jchild.remove();
};
});

// Find all slots.
Expand Down
95 changes: 95 additions & 0 deletions src/directives/sf-schema.directive.spec.js
Expand Up @@ -2882,4 +2882,99 @@ describe('destroy strategy', function() {
});
});

it('should remove added fields when refreshing or changing content', function (done) {
var a = {
schema: {
"type": "object",
"title": "Comment",
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"email": {
"title": "Email",
"type": "string",
"pattern": "^\\S+@\\S+$",
"description": "Email will be used for evil."
},
"comment": {
"title": "Comment",
"type": "string",
"maxLength": 20,
"validationMessage": "Don't be greedy!"
}
},
"required": [
"name",
"email",
"comment"
]
},
form: [
"name",
"email",
{
"key": "comment",
"type": "textarea",
"placeholder": "Make a comment"
},
{
"type": "submit",
"style": "btn-info",
"title": "OK"
}
]
};

var b = {
schema: {
"type": "object",
"title": "Types",
"properties": {
"string": {
"type": "string",
"minLength": 3
},
"integer": {
"type": "integer"
},
"number": {
"type": "number"
},
"boolean": {
"type": "boolean"
}
},
"required": [
"number"
]
},
form: [ "*" ]
};

inject(function ($compile, $rootScope) {
var scope = $rootScope.$new();
scope.model = {};
scope.schema = a.schema;
scope.form = a.form;

var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="model"></form>');

$compile(tmpl)(scope);
$rootScope.$apply();

tmpl.find('.form-group').length.should.be.eq(4);

scope.schema = b.schema;
scope.form = b.form;

scope.$broadcast('schemaFormRedraw');
$rootScope.$apply();

tmpl.find('.form-group').length.should.be.eq(3);

done();
});
});
});

0 comments on commit e52a3f2

Please sign in to comment.