Prevent error from unexpected attribute

This commit is contained in:
barsdeveloper
2023-01-04 22:16:51 +01:00
parent 3c5017de91
commit af44de4539
10 changed files with 65 additions and 39 deletions

View File

@@ -29,7 +29,10 @@ import VariableReferenceEntity from "../entity/VariableReferenceEntity"
import Vector2DEntity from "../entity/Vector2DEntity"
import VectorEntity from "../entity/VectorEntity"
/** @typedef {import ("../entity/IEntity").AttributeInformation} AttributeInformation */
/**
* @typedef {import ("../entity/IEntity").AttributeInformation} AttributeInformation
* @typedef {import ("../entity/IEntity").EntityConstructor} EntityConstructor
*/
let P = Parsimmon
@@ -150,17 +153,21 @@ export default class Grammar {
// Once the attribute name is known, look into entityType.attributes to get its type
const attributeKey = attributeName.split(".")
const attribute = Utility.objectGet(entityType.attributes, attributeKey)
let attributeValueGrammar =
attribute.constructor === Object && /** @type {AttributeInformation} */(attribute).serialized
const attributeValueGrammar = attribute // Remember attributeKey can not correspond to any attribute
? attribute.constructor === Object && /** @type {AttributeInformation} */(attribute).serialized
? r.String
: Grammar.getGrammarForType(r, attribute, r.AttributeAnyValue)
: r.AttributeAnyValue
// Returns a setter function for the attribute
return attributeValueGrammar.map(attributeValue =>
entity => Utility.objectSet(entity, attributeKey, attributeValue, true)
)
})
/** @param {Grammar} r */
/**
* @param {Grammar} r
* @param {EntityConstructor}
*/
static createEntityGrammar = (r, entityType, limitUnknownKeys = false) =>
P.seqMap(
entityType.lookbehind
@@ -181,10 +188,9 @@ export default class Grammar {
.chain(values => {
if (limitUnknownKeys) {
let unexpectedKeysCount = 0
let totalKeys = 0
let totalKeys = Object.keys(values)
for (const key in values) {
unexpectedKeysCount += key in entityType.attributes ? 0 : 1
++totalKeys
}
if (unexpectedKeysCount + 0.5 > Math.sqrt(totalKeys)) {
return P.fail()

View File

@@ -127,7 +127,7 @@ export default class ISerializer {
showProperty(entity, object, attributeKey, attributeValue) {
const attributes = /** @type {EntityConstructor} */(this.entityType).attributes
const attribute = Utility.objectGet(attributes, attributeKey)
if (attribute.constructor === Object) {
if (attribute?.constructor === Object) {
if (attribute.ignored) {
return false
}

View File

@@ -3,8 +3,6 @@ import ObjectEntity from "../entity/ObjectEntity"
import PinEntity from "../entity/PinEntity"
import SerializerFactory from "./SerializerFactory"
/** @typedef {import("../element/NodeElement").default} NodeElement */
export default class ObjectSerializer extends ISerializer {
constructor() {
@@ -33,7 +31,7 @@ export default class ObjectSerializer extends ISerializer {
/**
* @param {String} value
* @returns {NodeElement[]}
* @returns {ObjectEntity[]}
*/
readMultiple(value) {
const parseResult = ISerializer.grammar.MultipleObject.parse(value)