Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
types.forEach(type => {
const notComposite = !(type instanceof Composite);
const notUnion = !(type instanceof Union);
assert(notUnion, `Unions cannot contain other unions.`);
assert(
notComposite,
`Unions cannot contain composite types (given ${type.name}).`
);
// Ensure types can always be inferred by value.
if (type instanceof Derivation) {
assert(
!typeSet.has(type.subtype),
`Union contains ambiguous types (${type.name} & ${
type.subtype.name
}).`
);
}
});
types.forEach(type => {
const notComposite = !(type instanceof Composite);
const notUnion = !(type instanceof Union);
assert(notUnion, `Unions cannot contain other unions.`);
assert(
notComposite,
`Unions cannot contain composite types (given ${type.name}).`
);
// Ensure types can always be inferred by value.
if (type instanceof Derivation) {
assert(
!typeSet.has(type.subtype),
`Union contains ambiguous types (${type.name} & ${
type.subtype.name
}).`
);
}
});
constructor(name, primitive, def) {
assert(nameRegex.test(name), `Invalid derivation name "${name}".`);
this.name = name;
this.subtype = primitive;
Object.defineProperty(this, '_definition', {
value: def,
});
}
constructor(name: string, def: TypeDefinition) {
assert(nameRegex.test(name), invalidNameMsg(name));
this.name = name;
Object.defineProperties(this, {
_isValid: { value: def.isValid },
_coerce: { value: def.coerce },
});
}
migrateType(type) {
assert(
type.definition.hasOwnProperty(this.field),
`Field "${this.field}" doesn't exist in type ${type.name}.`
);
return {
defaultType: type.defaultType,
definition: {
...type.definition,
[this.field]: this.type,
},
};
}
dehydrate(hydrated) {
const value = this._definition.dehydrate(hydrated);
assert(
this.subtype.isValid(value),
`Serializer for type "${this.name}" returned an invalid value.`
);
return value;
}
migrateType(type) {
const definition = { ...type.definition };
const sourceExists = definition.hasOwnProperty(this.from);
const targetExists = definition.hasOwnProperty(this.to);
const fail = field =>
`Field "${field}" isn't defined in type ${type.name}.`;
assert(sourceExists, fail(this.from));
assert(targetExists, fail(this.to));
const target = definition[this.to];
const source = definition[this.from];
const targetComparison =
target instanceof Derivation ? target.subtype : target;
const sourceComparison =
source instanceof Derivation ? source.subtype : source;
const isSameType = targetComparison === sourceComparison;
assert(
isSameType,
`Can't move ${source.name} into ${target.name} ` +
`(${type.name} "${this.from}" -> "${this.to}").`
);
const fail = field =>
`Field "${field}" isn't defined in type ${type.name}.`;
assert(sourceExists, fail(this.from));
assert(targetExists, fail(this.to));
const target = definition[this.to];
const source = definition[this.from];
const targetComparison =
target instanceof Derivation ? target.subtype : target;
const sourceComparison =
source instanceof Derivation ? source.subtype : source;
const isSameType = targetComparison === sourceComparison;
assert(
isSameType,
`Can't move ${source.name} into ${target.name} ` +
`(${type.name} "${this.from}" -> "${this.to}").`
);
return {
defaultType: type.defaultType,
definition,
};
}
migrateType(type) {
assert(
this.field in type.definition,
`Can't remove field "${this.field}" from type ${type.name}; ` +
`No such field exists.`
);
const definition = { ...type.definition };
delete definition[this.field];
return {
defaultType: type.defaultType,
definition,
};
}
constructor(value: primitive) {
if (typeof value === 'number') {
assert(isFinite(value), `Literal(..) given invalid number (${value}).`);
}
super('literal', {
isValid: data => data === value,
coerce: () => value,
});
}
}