schema-tree.js 14.6 KB
Newer Older
jatuporn Tonggasem's avatar
jatuporn Tonggasem committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const error_1 = require("./error");
class InvalidSchema extends error_1.JsonSchemaErrorBase {
}
exports.InvalidSchema = InvalidSchema;
class InvalidValueError extends error_1.JsonSchemaErrorBase {
}
exports.InvalidValueError = InvalidValueError;
class MissingImplementationError extends error_1.JsonSchemaErrorBase {
}
exports.MissingImplementationError = MissingImplementationError;
class SettingReadOnlyPropertyError extends error_1.JsonSchemaErrorBase {
}
exports.SettingReadOnlyPropertyError = SettingReadOnlyPropertyError;
class InvalidUpdateValue extends error_1.JsonSchemaErrorBase {
}
exports.InvalidUpdateValue = InvalidUpdateValue;
/**
 * Holds all the information, including the value, of a node in the schema tree.
 */
class SchemaTreeNode {
    constructor(nodeMetaData) {
        this._defined = false;
        this._dirty = false;
        this._schema = nodeMetaData.schema;
        this._name = nodeMetaData.name;
        this._value = nodeMetaData.value;
        this._forward = nodeMetaData.forward;
        this._parent = nodeMetaData.parent;
    }
    dispose() {
        this._parent = null;
        this._schema = null;
        this._value = null;
        if (this._forward) {
            this._forward.dispose();
        }
        this._forward = null;
    }
    get defined() { return this._defined; }
    get dirty() { return this._dirty; }
    set dirty(v) {
        if (v) {
            this._defined = true;
            this._dirty = true;
            if (this._parent) {
                this._parent.dirty = true;
            }
        }
    }
    get value() { return this.get(); }
    get name() { return this._name; }
    get readOnly() { return this._schema['readOnly']; }
    get frozen() { return true; }
    get description() {
        return 'description' in this._schema ? this._schema['description'] : null;
    }
    get required() {
        if (!this._parent) {
            return false;
        }
        return this._parent.isChildRequired(this.name);
    }
    isChildRequired(_name) { return false; }
    get parent() { return this._parent; }
    get children() { return null; }
    get items() { return null; }
    get itemPrototype() { return null; }
    set(_v, _init = false, _force = false) {
        if (!this.readOnly) {
            throw new MissingImplementationError();
        }
        throw new SettingReadOnlyPropertyError();
    }
    isCompatible(_v) { return false; }
    static _defineProperty(proto, treeNode) {
        if (treeNode.readOnly) {
            Object.defineProperty(proto, treeNode.name, {
                enumerable: true,
                get: () => treeNode.get()
            });
        }
        else {
            Object.defineProperty(proto, treeNode.name, {
                enumerable: true,
                get: () => treeNode.get(),
                set: (v) => treeNode.set(v)
            });
        }
    }
}
exports.SchemaTreeNode = SchemaTreeNode;
/** Base Class used for Non-Leaves TreeNode. Meaning they can have children. */
class NonLeafSchemaTreeNode extends SchemaTreeNode {
    dispose() {
        for (const key of Object.keys(this.children || {})) {
            this.children[key].dispose();
        }
        for (let item of this.items || []) {
            item.dispose();
        }
        super.dispose();
    }
    get() {
        if (this.defined) {
            return this._value;
        }
        else {
            return undefined;
        }
    }
    destroy() {
        this._defined = false;
        this._value = null;
    }
    // Helper function to create a child based on its schema.
    _createChildProperty(name, value, forward, schema, define = true) {
        const type = ('oneOf' in schema) ? 'oneOf' :
            ('enum' in schema) ? 'enum' : schema['type'];
        let Klass = null;
        switch (type) {
            case 'object':
                Klass = ObjectSchemaTreeNode;
                break;
            case 'array':
                Klass = ArraySchemaTreeNode;
                break;
            case 'string':
                Klass = StringSchemaTreeNode;
                break;
            case 'boolean':
                Klass = BooleanSchemaTreeNode;
                break;
            case 'number':
                Klass = NumberSchemaTreeNode;
                break;
            case 'integer':
                Klass = IntegerSchemaTreeNode;
                break;
            case 'enum':
                Klass = EnumSchemaTreeNode;
                break;
            case 'oneOf':
                Klass = OneOfSchemaTreeNode;
                break;
            default:
                throw new InvalidSchema('Type ' + type + ' not understood by SchemaClassFactory.');
        }
        const metaData = new Klass({ parent: this, forward, value, schema, name });
        if (define) {
            SchemaTreeNode._defineProperty(this._value, metaData);
        }
        return metaData;
    }
}
exports.NonLeafSchemaTreeNode = NonLeafSchemaTreeNode;
class OneOfSchemaTreeNode extends NonLeafSchemaTreeNode {
    constructor(metaData) {
        super(metaData);
        let { value, forward, schema } = metaData;
        this._typesPrototype = schema['oneOf'].map((schema) => {
            return this._createChildProperty('', '', forward, schema, false);
        });
        this._currentTypeHolder = null;
        this._set(value, true, false);
    }
    _set(v, init, force) {
        if (!init && this.readOnly && !force) {
            throw new SettingReadOnlyPropertyError();
        }
        // Find the first type prototype that is compatible with the
        let proto = null;
        for (let i = 0; i < this._typesPrototype.length; i++) {
            const p = this._typesPrototype[i];
            if (p.isCompatible(v)) {
                proto = p;
                break;
            }
        }
        if (proto == null) {
            return;
        }
        if (!init) {
            this.dirty = true;
        }
        this._currentTypeHolder = proto;
        this._currentTypeHolder.set(v, false, true);
    }
    set(v, _init = false, force = false) {
        return this._set(v, false, force);
    }
    get() {
        return this._currentTypeHolder ? this._currentTypeHolder.get() : null;
    }
    get defaultValue() {
        return null;
    }
    get defined() { return this._currentTypeHolder ? this._currentTypeHolder.defined : false; }
    get items() { return this._typesPrototype; }
    get type() { return 'oneOf'; }
    get tsType() { return null; }
    serialize(serializer) { serializer.outputOneOf(this); }
}
exports.OneOfSchemaTreeNode = OneOfSchemaTreeNode;
/** A Schema Tree Node that represents an object. */
class ObjectSchemaTreeNode extends NonLeafSchemaTreeNode {
    constructor(metaData) {
        super(metaData);
        this._frozen = false;
        this._set(metaData.value, true, false);
    }
    _set(value, init, force) {
        if (!init && this.readOnly && !force) {
            throw new SettingReadOnlyPropertyError();
        }
        const schema = this._schema;
        const forward = this._forward;
        this._defined = !!value;
        this._children = Object.create(null);
        this._value = Object.create(null);
        this._dirty = this._dirty || !init;
        if (schema['properties']) {
            for (const name of Object.keys(schema['properties'])) {
                const propertySchema = schema['properties'][name];
                this._children[name] = this._createChildProperty(name, value ? value[name] : undefined, forward ? forward.children[name] : null, propertySchema);
            }
        }
        else if (!schema['additionalProperties']) {
            throw new InvalidSchema('Schema does not have a properties, but doesnt allow for '
                + 'additional properties.');
        }
        if (!schema['additionalProperties']) {
            this._frozen = true;
            Object.freeze(this._value);
            Object.freeze(this._children);
        }
        else if (value) {
            // Set other properties which don't have a schema.
            for (const key of Object.keys(value)) {
                if (!this._children[key]) {
                    this._value[key] = value[key];
                }
            }
        }
    }
    set(v, force = false) {
        return this._set(v, false, force);
    }
    get frozen() { return this._frozen; }
    get children() { return this._children; }
    get type() { return 'object'; }
    get tsType() { return Object; }
    get defaultValue() { return null; }
    isCompatible(v) { return typeof v == 'object' && v !== null; }
    isChildRequired(name) {
        if (this._schema['required']) {
            return this._schema['required'].indexOf(name) != -1;
        }
        return false;
    }
    serialize(serializer) { serializer.object(this); }
}
exports.ObjectSchemaTreeNode = ObjectSchemaTreeNode;
/** A Schema Tree Node that represents an array. */
class ArraySchemaTreeNode extends NonLeafSchemaTreeNode {
    constructor(metaData) {
        super(metaData);
        this._set(metaData.value, true, false);
        // Keep the item's schema as a schema node. This is important to keep type information.
        this._itemPrototype = this._createChildProperty('', undefined, null, metaData.schema['items'], false);
    }
    _set(value, init, _force) {
        const schema = this._schema;
        const forward = this._forward;
        this._value = Object.create(null);
        this._dirty = this._dirty || !init;
        if (value) {
            this._defined = true;
        }
        else {
            this._defined = false;
            value = [];
        }
        this._items = [];
        this._value = [];
        for (let index = 0; index < value.length; index++) {
            this._items[index] = this._createChildProperty('' + index, value && value[index], forward && forward.items[index], schema['items']);
        }
    }
    set(v, init = false, force = false) {
        return this._set(v, init, force);
    }
    isCompatible(v) { return Array.isArray(v); }
    get type() { return 'array'; }
    get tsType() { return Array; }
    get items() { return this._items; }
    get itemPrototype() { return this._itemPrototype; }
    get defaultValue() { return null; }
    serialize(serializer) { serializer.array(this); }
}
exports.ArraySchemaTreeNode = ArraySchemaTreeNode;
/**
 * The root class of the tree node. Receives a prototype that will be filled with the
 * properties of the Schema root.
 */
class RootSchemaTreeNode extends ObjectSchemaTreeNode {
    constructor(proto, metaData) {
        super(metaData);
        for (const key of Object.keys(this._children)) {
            if (this._children[key]) {
                SchemaTreeNode._defineProperty(proto, this._children[key]);
            }
        }
    }
}
exports.RootSchemaTreeNode = RootSchemaTreeNode;
/** A leaf in the schema tree. Must contain a single primitive value. */
class LeafSchemaTreeNode extends SchemaTreeNode {
    constructor(metaData) {
        super(metaData);
        this._defined = metaData.value !== undefined;
        if ('default' in metaData.schema) {
            this._default = this.convert(metaData.schema['default']);
        }
    }
    get() {
        if (!this.defined && this._forward) {
            return this._forward.get();
        }
        if (!this.defined) {
            return 'default' in this._schema ? this._default : undefined;
        }
        return this._value === undefined
            ? undefined
            : (this._value === null ? null : this.convert(this._value));
    }
    set(v, init = false, force = false) {
        if (this.readOnly && !force) {
            throw new SettingReadOnlyPropertyError();
        }
        let convertedValue = this.convert(v);
        if (convertedValue === null || convertedValue === undefined) {
            if (this.required) {
                throw new InvalidValueError(`Invalid value "${v}" on a required field.`);
            }
        }
        this.dirty = !init;
        this._value = convertedValue;
    }
    destroy() {
        this._defined = false;
        this._value = null;
    }
    get defaultValue() {
        return this.hasDefault ? this._default : null;
    }
    get hasDefault() {
        return 'default' in this._schema;
    }
    serialize(serializer) {
        serializer.outputValue(this);
    }
}
exports.LeafSchemaTreeNode = LeafSchemaTreeNode;
/** Basic primitives for JSON Schema. */
class StringSchemaTreeNode extends LeafSchemaTreeNode {
    serialize(serializer) { serializer.outputString(this); }
    isCompatible(v) { return typeof v == 'string' || v instanceof String; }
    convert(v) { return v === undefined ? undefined : '' + v; }
    get type() { return 'string'; }
    get tsType() { return String; }
}
class EnumSchemaTreeNode extends LeafSchemaTreeNode {
    constructor(metaData) {
        super(metaData);
        if (!Array.isArray(metaData.schema['enum'])) {
            throw new InvalidSchema();
        }
        if (this.hasDefault && !this._isInEnum(this._default)) {
            throw new InvalidSchema();
        }
        this.set(metaData.value, true, true);
    }
    _isInEnum(value) {
        return this._schema['enum'].some((v) => v === value);
    }
    get items() { return this._schema['enum']; }
    set(value, init = false, force = false) {
        if (!(value === undefined || this._isInEnum(value))) {
            throw new InvalidUpdateValue('Invalid value can only be one of these: ' + this.items);
        }
        super.set(value, init, force);
    }
    isCompatible(v) {
        return this._isInEnum(v);
    }
    convert(v) {
        if (v === undefined) {
            return undefined;
        }
        if (!this._isInEnum(v)) {
            return undefined;
        }
        return v;
    }
    get type() {
        return this._schema['type'] || 'any';
    }
    get tsType() { return null; }
    serialize(serializer) { serializer.outputEnum(this); }
}
class BooleanSchemaTreeNode extends LeafSchemaTreeNode {
    serialize(serializer) { serializer.outputBoolean(this); }
    isCompatible(v) { return typeof v == 'boolean' || v instanceof Boolean; }
    convert(v) { return v === undefined ? undefined : !!v; }
    get type() { return 'boolean'; }
    get tsType() { return Boolean; }
}
class NumberSchemaTreeNode extends LeafSchemaTreeNode {
    serialize(serializer) { serializer.outputNumber(this); }
    isCompatible(v) { return typeof v == 'number' || v instanceof Number; }
    convert(v) { return v === undefined ? undefined : +v; }
    get type() { return 'number'; }
    get tsType() { return Number; }
}
class IntegerSchemaTreeNode extends NumberSchemaTreeNode {
    convert(v) { return v === undefined ? undefined : Math.floor(+v); }
}
//# sourceMappingURL=/users/hansl/sources/angular-cli/src/schema-tree.js.map