Skip to content

Commit

Permalink
Added allowRealPubicId to ving schema props.
Browse files Browse the repository at this point in the history
* Fixed: filter on id #203
* Fixed: How to do a search for hardware ticket by ticket number? #29
  • Loading branch information
rizen committed Jan 13, 2025
1 parent c5beb9d commit 1362873
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
3 changes: 3 additions & 0 deletions ving/docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Also don't forget to update the imports in your schema files.
* Set filterQualifier to true for the baseSchemaId.
* Added better error handling for int2str parseId().
* Added stringToNumber option to int2str parseId().
* Added allowRealPubicId to ving schema props.
* Fixed: filter on id #203
* Fixed: How to do a search for hardware ticket by ticket number? #29

## December 2024

Expand Down
10 changes: 10 additions & 0 deletions ving/docs/subsystems/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ await users.search();
onBeforeRouteLeave(() => users.dispose());
```
#### Filtering
You can filter the list of records by passing in values to the query object. For example, to filter by an exact value:
```js
users.query.id = 'xxx';
```
For more on filtering see the [rest api](rest#filters) documentation.
### useVingRecord()
A client for interacting with [server-side ving records](ving-record#record-api) through the [Rest API](rest).
Expand Down
7 changes: 7 additions & 0 deletions ving/docs/subsystems/ving-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ An optional boolean that if true will allow searching via the [rest api](rest) f
An optional boolean that if true will allow searching via the [rest api](rest) for range matching against this field. This is an alternative to overriding the `describeListFilter()` method in [VingRecord](ving-record). Only use on `int` and `date` type props.


##### allowRealPubicId

An optional boolean that if true will do 2 things if added to an `id` prop `type`:

* Allows searching via the [rest api](rest) for exact match filtering against this id using its integer value in addiont to the encrypted string assuming `filterQualifier` is true.
* Adds the real id to the `meta.realId` object in the [rest api](rest) response.

##### autoUpdate

The `autoUpdate` field is an optional boolean that is only used on a prop with type `date`. If `true` the date will automatically get set every time `update()` is called on the record. This is generally never needed by anything other than the built in `dateUpdated` record that every record already has.
Expand Down
13 changes: 11 additions & 2 deletions ving/record/VingRecord.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ export class VingRecord {
}

for (const field of schema.props) {

// meta
if (isObject(out.meta)
&& include.meta
) {
if (field.allowRealPubicId) {
if (!isObject(out.meta.realId)) out.meta.realId = {};
out.meta.realId[field.name] = this.#props[field.name];
}
}

if (field.name == 'id') // already done
continue;

Expand All @@ -214,8 +225,6 @@ export class VingRecord {
|| (currentUser?.isaRole(roles));
if (!visible) continue;

const fieldName = field.name.toString();

// props
if (field.type == 'id')
out.props[field.name] = stringifyId(this.#props[field.name], field.relation?.kind || schema.kind);
Expand Down
23 changes: 22 additions & 1 deletion ving/schema/validator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const validateProps = (schema) => {
validatePropFilterQuery(prop, schema);
validatePropFilterQualifier(prop, schema);
validatePropAutoUpdate(prop, schema);
validatePropAllowRealPubicId(prop, schema);
}
}

Expand Down Expand Up @@ -508,7 +509,7 @@ export const validatePropFilterQualifier = (prop, schema) => {
return;
if (!isBoolean(prop.filterQualifier))
throw ving.ouch(442, `${formatPropPath(prop, schema)}.filterQualifier must be a boolean.`);
if (prop.type == 'id' && prop.relation?.type != 'parent')
if (prop.type == 'id' && prop.relation && prop.relation?.type != 'parent')
throw ving.ouch(442, `${formatPropPath(prop, schema)}.filterQualifier can only exist on type id if the relation.type is parent.`);
}

Expand All @@ -532,6 +533,26 @@ export const validatePropFilterRange = (prop, schema) => {
throw ving.ouch(442, `${formatPropPath(prop, schema)}.filterRange must be a boolean.`);
}

/**
* Validates the allowRealPubicId field of a prop.
* @param {object} prop The prop schema to check against.
* @param {VingSchema} schema The schema to check against.
* @throws 442 if some attribute is outside of the normal definition
* @example
* validatePropAllowRealPubicId(prop, schema)
*/
export const validatePropAllowRealPubicId = (prop, schema) => {
if (!['id'].includes(prop.type)) {
if ('allowRealPubicId' in prop)
throw ving.ouch(442, `${formatPropPath(prop, schema)}.allowRealPubicId should not exist.`);
return;
}
if (!('allowRealPubicId' in prop)) // not required
return;
if (!isBoolean(prop.allowRealPubicId))
throw ving.ouch(442, `${formatPropPath(prop, schema)}.allowRealPubicId must be a boolean.`);
}

/**
* Validates the required field of a prop.
* @param {object} prop The prop schema to check against.
Expand Down
2 changes: 1 addition & 1 deletion ving/utils/rest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const fixColumnData = (column, data) => {
return data == 'true' ? true : false;
}
if (column.vingSchemaProp.type == 'id') {
return parseId(data);
return parseId(data, { stringToNumber: column.vingSchemaProp.allowRealPubicId ? true : false });
}
if (column.vingSchemaProp.type == 'int') {
return Number(data);
Expand Down

0 comments on commit 1362873

Please sign in to comment.