Skip to content
This repository has been archived by the owner on Apr 9, 2022. It is now read-only.

Commit

Permalink
feat(@angular-devkit/core): add support for schema in template
Browse files Browse the repository at this point in the history
Before we were messing with ownKeys to only return defined properties, but that
has the side effect that those will not work inside a "with() {}" clause, which
is what templates are using.

We now need a serialize symbol if we want to only serialize defined properties,
but for now parity with @ngtools/json-schema is not a priority.
  • Loading branch information
hansl committed Sep 26, 2017
1 parent 3842600 commit 494f87d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ const handlers = Object.create(null);
}
%>

const objectFunctions = {
hasOwnProperty(name) { return objectProxyHandler.has(null, name); },
};


let defined = false;
const proxy = new Proxy({}, {
const objectProxyHandler = {
isExtensible() { return false; },
has(target, prop) {
return (prop in handlers && handlers[prop].isDefined())
Expand All @@ -54,6 +58,9 @@ const proxy = new Proxy({}, {
if (prop in handlers) {
return handlers[prop].get();
}
if (prop in objectFunctions) {
return objectFunctions[prop];
}
return undefined;
},
set(target, prop, v) {
Expand Down Expand Up @@ -90,15 +97,20 @@ const proxy = new Proxy({}, {
getOwnPropertyDescriptor(target, prop) {
if (prop in handlers) {
return { configurable: true, enumerable: true };
} else if (additionalPropertyHandler && prop in additionalPropertyHandler) {
return { configurable: true, enumerable: true };
}
},
ownKeys(target) {
return [].concat(
Object.keys(handlers).filter(function(key) { return handlers[key].isDefined(); }),
Object.keys(handlers),
additionalPropertyHandler ? Object.keys(additionalProperties) : []
);
},
});
};


const proxy = new Proxy({}, objectProxyHandler);

const objectHandler = {
set(v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function works(registry: schema.JsonSchemaRegistry, schema: any) {
v.objectKey1.objectKey = { stringKey: 'str2' };
expect(v.objectKey1.objectKey.stringKey).toBe('str2');

expect(Object.keys(v.objectKey1)).toEqual(['stringKey', 'objectKey']);
expect(Object.keys(v.objectKey1)).toEqual(['stringKey', 'stringKeyDefault', 'objectKey']);
}


Expand All @@ -55,9 +55,8 @@ export function accessUndefined(registry: schema.JsonSchemaRegistry, schema: any
expect(v.objectKey1).not.toBe(undefined);
expect(v.objectKey1.stringKey).toBe('hello');
expect(v.objectKey1.numberKey).toBe(undefined);
expect(v).toEqual({ 'requiredKey': 1, 'objectKey1': { 'stringKey': 'hello' } });
v.objectKey1.stringKey = undefined;
expect(v).toEqual({ 'requiredKey': 1 });
expect(v.objectKey1.stringKey).toBe(undefined);

expect(v.stringKeyDefault).toBe('defaultValue');
expect(v.objectKey1.stringKeyDefault).toBe('defaultValue2');
Expand Down

0 comments on commit 494f87d

Please sign in to comment.