Skip to content

Commit

Permalink
feat(validator): additionalProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Jan 3, 2023
1 parent 7c02422 commit 60829a6
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions core/validator/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,50 @@ export {JsonSchema};
export function validator<T extends ValidType>(
validSchema: JsonSchema,
targetObject: Record<string, unknown>,
additionalProperties = false,
path = '.',
): T {
if (typeof targetObject !== 'object' || targetObject == null) {
throw new Error('invalid_type', {
cause: {
message: 'targetObject is not a function or null',
itemPath: path,
itemSchema: 'JsonSchema',
itemValue: String(targetObject),
},
});
}

for (const itemName in targetObject) {
if (!Object.prototype.hasOwnProperty.call(targetObject, itemName)) continue;
if (
additionalProperties === false &&
Object.keys(validSchema).sort().join() !== Object.keys(targetObject).sort().join()
) {
throw new Error('invalid_type', {
cause: {
message: 'Object.keys(validSchema) !== Object.keys(targetObject)',
itemPath: path,
itemSchema: String(validSchema),
itemValue: String(targetObject),
},
});
}

for (const itemName in validSchema) {
if (!Object.prototype.hasOwnProperty.call(validSchema, itemName)) continue;

const itemPath = `${path}/${itemName}`;
const itemSchema = validSchema[itemName];
const itemValue = targetObject[itemName] as string | number | boolean | Record<string, unknown>;

if (itemSchema == null) {
throw new Error('invalid_type', {
cause: {
itemPath,
itemSchema: 'undefined',
itemValue: String(itemValue),
},
});
}

else if (typeof itemSchema === 'object') {
if (typeof itemSchema === 'object' && itemSchema != null) {
// nested object
targetObject[itemName] = validator<ValidType>(
itemSchema,
itemValue as Record<string, unknown>,
itemPath,
itemValue as Record<string, unknown>,
additionalProperties,
itemPath,
);
}

else if (itemSchema === Boolean) {
const strValue = String(itemValue).toLowerCase();
if (strValue === 'true') {
Expand All @@ -56,36 +62,34 @@ export function validator<T extends ValidType>(
else {
throw new Error('invalid_type', {
cause: {
message: 'invalid type',
itemPath,
itemSchema: 'Boolean',
itemValue: String(itemValue),
},
});
}
}

else if (itemSchema === Number) {
if (isNumber(itemValue)) {
targetObject[itemName] = +itemValue;
}
else {
throw new Error('invalid_type', {
cause: {
message: 'invalid type',
itemPath,
itemSchema: 'Number',
itemValue: String(itemValue),
},
});
}
}

else if (itemSchema === String) {
if (typeof itemValue === 'string') {
targetObject[itemName] = itemValue;
}
else {
if (typeof itemValue !== 'string') {
throw new Error('invalid_type', {
cause: {
message: 'invalid type',
itemPath,
itemSchema: 'String',
itemValue: String(itemValue),
Expand Down

0 comments on commit 60829a6

Please sign in to comment.