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AK,CAAC,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,WAAW,IAAI,KAAK,KAAK,CAAC,WAAW,CAAC,IAAI,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC;QACrH,MAAM,IAAI,iBAAiB,CAAC,MAAM,YAAY,uBAAuB,UAAU,oBAAoB,KAAK,CAAC,WAAW,CAAC,IAAI,IAAI,KAAK,EAAE,CAAC,CAAC;IAC1I,CAAC;AACL,CAAC;AAED,SAAgB,kBAAkB,CAC9B,KAAU,EACV,IAIsB,EACtB,QAAa,EACb,KAAsB;IAEtB,IAAI,CAAC,CAAC,KAAK,YAAY,IAAI,CAAC,EAAE,CAAC;QAC3B,MAAM,IAAI,iBAAiB,CAAC,MAAM,IAAI,CAAC,IAAI,wBAAwB,KAAK,IAAK,KAAa,CAAC,WAAW,CAAC,IAAI,qBAAqB,QAAQ,CAAC,WAAW,CAAC,IAAI,IAAI,KAAK,EAAE,CAAC,CAAC;IAC1K,CAAC;AACL,CAAC","sourcesContent":["import type { Schema } from \"../Schema\";\nimport type { CollectionSchema } from \"../types/custom/CollectionSchema\";\nimport type { MapSchema } from \"../types/custom/MapSchema\";\nimport type { SetSchema } from \"../types/custom/SetSchema\";\nimport type { ArraySchema } from \"../types/custom/ArraySchema\";\nimport type { Ref } from \"../encoder/ChangeTree\";\n\nexport class EncodeSchemaError extends Error {}\n\nexport function assertType(value: any, type: string, klass: Schema, field: string | number) {\n    let typeofTarget: string;\n    let allowNull: boolean = false;\n\n    switch (type) {\n        case \"number\":\n        case \"int8\":\n        case \"uint8\":\n        case \"int16\":\n        case \"uint16\":\n        case \"int32\":\n        case \"uint32\":\n        case \"int64\":\n        case \"uint64\":\n        case \"float32\":\n        case \"float64\":\n            typeofTarget = \"number\";\n            if (isNaN(value)) {\n                console.log(`trying to encode \"NaN\" in ${klass.constructor.name}#${field}`);\n            }\n            break;\n        case \"bigint64\":\n        case \"biguint64\":\n            typeofTarget = \"bigint\";\n            break;\n        case \"string\":\n            typeofTarget = \"string\";\n            allowNull = true;\n            break;\n        case \"boolean\":\n            // boolean is always encoded as true/false based on truthiness\n            return;\n        default:\n            // skip assertion for custom types\n            // TODO: allow custom types to define their own assertions\n            return;\n    }\n\n    if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) {\n        let foundValue = `'${JSON.stringify(value)}'${(value && value.constructor && ` (${value.constructor.name})`) || ''}`;\n        throw new EncodeSchemaError(`a '${typeofTarget}' was expected, but ${foundValue} was provided in ${klass.constructor.name}#${field}`);\n    }\n}\n\nexport function assertInstanceType(\n    value: Ref,\n    type: typeof Schema\n        | typeof ArraySchema\n        | typeof MapSchema\n        | typeof CollectionSchema\n        | typeof SetSchema,\n    instance: Ref,\n    field: string | number,\n) {\n    if (!(value instanceof type)) {\n        throw new EncodeSchemaError(`a '${type.name}' was expected, but '${value && (value as any).constructor.name}' was provided in ${instance.constructor.name}#${field}`);\n    }\n}"]}