Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/loose-dryers-lead.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@journeyapps-labs/micro-codecs': patch
'@journeyapps-labs/micro-schema': patch
---

Validate ObjectId and ResourceId.id match bson ObjectId format.
4 changes: 2 additions & 2 deletions packages/codecs/src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as t from 'ts-codec';
export const ObjectIdParser = t.createParser<typeof codecs.ObjectId>(codecs.ObjectId._tag, (_, { target }) => {
switch (target) {
case t.TransformTarget.Encoded: {
return { type: 'string' };
return { type: 'string', bsonObjectId: true };
}
case t.TransformTarget.Decoded: {
return { bsonType: 'ObjectId' };
Expand All @@ -18,7 +18,7 @@ export const ResourceIdParser = t.createParser<typeof codecs.ResourceId>(codecs.
return {
type: 'object',
properties: {
id: { type: 'string' }
id: { type: 'string', bsonObjectId: true }
},
required: ['id']
};
Expand Down
2 changes: 2 additions & 0 deletions packages/codecs/tests/__snapshots__/parsers.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ exports[`parsers > should correctly generate ObjectId schemas 1`] = `
"definitions": {},
},
"encoded": {
"bsonObjectId": true,
"definitions": {},
"type": "string",
},
Expand All @@ -152,6 +153,7 @@ exports[`parsers > should correctly generate ResourceId schemas 1`] = `
"definitions": {},
"properties": {
"id": {
"bsonObjectId": true,
"type": "string",
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@journeyapps-labs/micro-errors": "workspace:^",
"ajv": "^8.17.1",
"better-ajv-errors": "^2.0.2",
"bson": "^6.10.4",
"ts-codec": "^1.3.0",
"zod": "^4.1.12"
},
Expand Down
16 changes: 16 additions & 0 deletions packages/schema/src/json-schema/keywords.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import * as ajv from 'ajv';
import { ObjectId } from 'bson';

export const ObjectIdKeyword: ajv.KeywordDefinition = {
keyword: 'bsonObjectId',
metaSchema: { type: 'boolean' },
error: {
message: 'should be a valid ObjectId string'
},
code(context) {
const fn = context.gen.scopeValue('func', {
ref: ObjectId.isValid,
code: ajv._`require('bson').ObjectId.isValid`
});
context.fail(ajv._`!${fn}(${context.data})`);
}
};

export const BufferNodeType: ajv.KeywordDefinition = {
keyword: 'nodeType',
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/validators/schema-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const createSchemaValidator = <T = any>(
try {
const ajv = new AJV({
allErrors: !(params.fail_fast ?? false),
keywords: [keywords.BufferNodeType],
keywords: [keywords.BufferNodeType, keywords.ObjectIdKeyword],
...(params.ajv || {})
});

Expand Down
17 changes: 17 additions & 0 deletions packages/schema/tests/schema-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, test, it, expect } from 'vitest';

import base_schema from './fixtures/schema';
import * as micro_schema from '../src';
import * as codecs from '@journeyapps-labs/micro-codecs';

const base_validator = micro_schema.createSchemaValidator(base_schema);

Expand Down Expand Up @@ -154,6 +155,22 @@ describe('json-schema-validation', () => {
expect(res2).toMatchSnapshot();
});

it('should validate bsonObjectId fields for ObjectId', () => {
const validator = micro_schema.createTsCodecValidator(codecs.ObjectId);

expect(validator.validate('507f1f77bcf86cd799439011').valid).toBe(true);
expect(validator.validate('not-an-objectid').valid).toBe(false);
expect(validator.validate('507f1f77bcf86cd79943901').valid).toBe(false);
});

it('should validate bsonObjectId fields for ResourceId', () => {
const validator = micro_schema.createTsCodecValidator(codecs.ResourceId);

expect(validator.validate({ id: '507f1f77bcf86cd799439011' }).valid).toBe(true);
expect(validator.validate({ id: 'not-an-objectid' }).valid).toBe(false);
expect(validator.validate({ id: '507f1f77bcf86cd79943901' }).valid).toBe(false);
});

it('should fail to compile invalid node types', () => {
try {
micro_schema.createSchemaValidator({
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading